我想從一個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。
謝謝!
感謝您的回覆!我試過了,pycurl似乎很有用。但龍捲風期望pycurl返回頭部和身體作爲字符串(現在他們是字節,因爲他們應該!)。所以看起來龍捲風不像python3,只需要很少的修改就可以在python3下運行。我不是Python的專家,所以我選擇放棄並使用簡單的http客戶端。不管怎樣,謝謝你! – riaqn
其餘的Tornado在Python 3上運行良好;這只是curl_httpclient有問題。這些將在支持Python 3的官方pycurl發佈後得到修復。 –
http://pycurl.sourceforge.net/doc/release-notes.html看起來像它已被更新到據說支持python3 – proteneer