2013-09-22 54 views
2

我想从一个website.So获取一个图像(GIF格式),我用龙卷风,建立异步HTTP客户端做it.My代码是这样的:龙卷风卷曲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文本,这是unnecessary.IF我评论

tornado.httpclient.AsyncHTTPClient.configure( “tornado.curl_httpclient.CurlAsyncHTTPClient” )

出来,将使用一个简单的HTTP客户端而不是pycurl,效果很好。(它告诉我的“内容”的类型是字节)

因此,如果它返回一个字节对象,为什么它会尝试解码它?我认为问题是pycurl或pycurl在龙卷风中的包装,对吧?

我的python版本是3.2.5,龙卷风3.1.1,pycurl 7.19。

谢谢!

回答

2

pycurl 7.19不支持Python 3. Ubuntu(也可能是其他Linux发行版)发布了一个修改后的pycurl版本,部分与Python 3一起工作,但它不适用于Tornado(https://github.com/facebook/tornado/issues/671),并且失败看起来像你在这里看到的异常。

,直到有pycurl的新版本正式支持Python 3(或您使用的变化在龙卷风bug报告所建议的),恐怕你要么需要回去到Python 2.7或使用龙卷风的simple_httpclient代替。

+0

感谢您的回复!我试过了,pycurl似乎很有用。但龙卷风期望pycurl返回头部和身体作为字符串(现在他们是字节,因为他们应该!)。所以看起来龙卷风不像python3,只需要很少的修改就可以在python3下运行。我不是Python的专家,所以我选择放弃并使用简单的http客户端。不管怎样,谢谢你! – riaqn

+0

其余的Tornado在Python 3上运行良好;这只是curl_httpclient有问题。这些将在支持Python 3的官方pycurl发布后得到修复。 –

+0

http://pycurl.sourceforge.net/doc/release-notes.html看起来像它已被更新到据说支持python3 – proteneer