python - Tornado curl http客户端无法获取二进制文件

标签 python tornado libcurl pycurl asynchttpclient

我想从网站获取图像(GIF格式)。所以我使用tornado内置异步http客户端来做到这一点。我的代码如下:

import tornado.httpclient
import tornado.ioloop
import tornado.gen
import tornado.web

tornado.httpclient.AsyncHTTPClient.configure("tornado.curl_httpclient.CurlAsyncHTTPClient")
http_client = tornado.httpclient.AsyncHTTPClient()

class test(tornado.web.RequestHandler):
    @tornado.gen.coroutine
    def get(self):
        content = yield http_client.fetch('http://www.baidu.com/img/bdlogo.gif')
        print('=====', type(content.body))

application = tornado.web.Application([
    (r'/', test)
    ])
application.listen(80)
tornado.ioloop.IOLoop.instance().start()

所以当我访问服务器时,它应该获取一个 gif 文件。但是它捕获了一个异常。

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x81 in position 8: invalid start byte
ERROR:tornado.application:Uncaught exception GET / (127.0.0.1)
HTTPRequest(protocol='http', host='127.0.0.1', method='GET', uri='/', version='HTTP/1.1', remote_ip='127.0.0.1', headers={'Accept-Language': 'zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3', 'Accept-Encoding': 'gzip, deflate', 'Host': '127.0.0.1', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'User-Agent': 'Mozilla/5.0 (X11; Linux i686; rv:17.0) Gecko/20130922 Firefox/17.0', 'Connection': 'keep-alive', 'Cache-Control': 'max-age=0', 'If-None-Match': '"da39a3ee5e6b4b0d3255bfef95601890afd80709"'})
Traceback (most recent call last):
  File "/usr/lib/python3.2/site-packages/tornado/web.py", line 1144, in _when_complete
    if result.result() is not None:
  File "/usr/lib/python3.2/site-packages/tornado/concurrent.py", line 129, in result
    raise_exc_info(self.__exc_info)
  File "<string>", line 3, in raise_exc_info
  File "/usr/lib/python3.2/site-packages/tornado/stack_context.py", line 302, in wrapped
    ret = fn(*args, **kwargs)
  File "/usr/lib/python3.2/site-packages/tornado/gen.py", line 550, in inner
    self.set_result(key, result)
  File "/usr/lib/python3.2/site-packages/tornado/gen.py", line 476, in set_result
    self.run()
  File "/usr/lib/python3.2/site-packages/tornado/gen.py", line 505, in run
    yielded = self.gen.throw(*exc_info)
  File "test.py", line 12, in get
    content = yield http_client.fetch('http://www.baidu.com/img/bdlogo.gif')
  File "/usr/lib/python3.2/site-packages/tornado/gen.py", line 496, in run
    next = self.yield_point.get_result()
  File "/usr/lib/python3.2/site-packages/tornado/gen.py", line 395, in get_result
    return self.runner.pop_result(self.key).result()
  File "/usr/lib/python3.2/concurrent/futures/_base.py", line 393, in result
    return self.__get_result()
  File "/usr/lib/python3.2/concurrent/futures/_base.py", line 352, in __get_result
    raise self._exception
tornado.curl_httpclient.CurlError: HTTP 599: Failed writing body (0 != 1024)
ERROR:tornado.access:500 GET / (127.0.0.1) 131.53ms

它似乎试图将我的二进制文件解码为 UTF-8 文本,这是不必要的。如果我评论

tornado.httpclient.AsyncHTTPClient.configure("tornado.curl_httpclient.CurlAsyncHTTPClient")

out,它将使用一个简单的http客户端而不是pycurl,它运行良好。(它告诉我“内容”的类型是字节)

那么如果它返回一个字节对象,为什么它会尝试解码它?我认为问题出在tornado中的pycurl或pycurl的包装上,对吗?

我的python版本是3.2.5,tornado 3.1.1,pycurl 7.19。

谢谢!

最佳答案

pycurl 7.19 不支持 Python 3。Ubuntu(可能还有其他 Linux 发行版)提供了 pycurl 的修改版本,该版本部分适用于 Python 3,但不适用于 Tornado ( https://github.com/facebook/tornado/issues/671 ),并且失败并显示一个异常(exception),看起来就像您在这里看到的那样。

在出现正式支持 Python 3 的新版本 pycurl 之前(或者您使用 Tornado bug 报告中建议的更改),恐怕您需要返回到 Python 2.7 或使用 Tornado 的 simple_httpclient。

关于python - Tornado curl http客户端无法获取二进制文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18944275/

相关文章:

c - POSTFIELD 应该是什么样子的?

c - 指定 C 库的包含目录

python - 如何从 kdeplot 获取半高全宽 (FWHM)

Mac OS X 中的 Python Errno 9 错误文件描述符

python - 运行 Tornado AsyncHTTPTestCase 时获取错误文件描述符

python - Asyncmongo 和 Twisted

php - 使用 PHP 的 CURL - 非常慢

python - 值错误: could not convert string to float: 'lisans' in Python

python - OpenCV-用矩形扫描图像

python - 拆分包含字符串和整数的列表