当前位置: 代码迷 >> 综合 >> tornado curl_httpclient proxy
  详细解决方案

tornado curl_httpclient proxy

热度:63   发布时间:2024-01-16 23:53:57.0

tornado httpclient

默认simple_httpclient,满足大部分普通需求.

curl_httpclient更多特性(需要libcurl pycurl):

  • 支持http proxy,以及使用指定网络接口

  • 不死磕http规范,兼容更多站点

  • 效率更高

curl_httpclient 使用proxy实例

以下代码执行的机器通过代理访问网络

>>> import tornado
>>> tornado.version
'4.2.1'
>>> from tornado.httpclient import HTTPClient
>>> from tornado.httpserver import HTTPRequest
>>> c = HTTPClient()
>>> req = HTTPRequest(url="http://t.cp31.ott.cibntv.net/XXXX", follow_redirects=False, proxy_host="10.103.11.11", proxy_port=81)
>>> c.fetch(req)
Traceback (most recent call last):File "", line 1, in File "/opt/python2.7/lib/python2.7/site-packages/tornado-4.2.1-py2.7-linux-x86_64.egg/tornado/httpclient.py", line 102, in fetchself._async_client.fetch, request, **kwargs))File "/opt/python2.7/lib/python2.7/site-packages/tornado-4.2.1-py2.7-linux-x86_64.egg/tornado/ioloop.py", line 445, in run_syncreturn future_cell[0].result()File "/opt/python2.7/lib/python2.7/site-packages/tornado-4.2.1-py2.7-linux-x86_64.egg/tornado/concurrent.py", line 215, in resultraise_exc_info(self._exc_info)File "", line 3, in raise_exc_info
tornado.httpclient.HTTPError: HTTP 599: Timeout#simple_httpclient 不支持代理,599超时>>> from tornado.curl_httpclient import CurlAsyncHTTPClient
>>> from tornado.ioloop import IOLoop
>>> c = CurlAsyncHTTPClient()
>>> def print_response(r):
...     print r.error or r.body
... 
>>> c.fetch(req, print_response)
>>> IOLoop.instance().start()
HTTP 301: Moved Permanently
ERROR:tornado.application:Future exception was never retrieved: HTTPError: HTTP 301: Moved Permanently# curl_httpclient使用代理成功联网,取得该URL的301信息


所以有童鞋说curl可以访问某个URL,证明网络通畅,就是tornado代码无法访问该URL,大概原因就是如此了.调试步骤

  • curl 能访问,说明网络通畅(不论是否有走代理)

  • urllib2确认一下Python代码能访问,如果urllib2都能访问,tornado不行,那极大概率是上面所述

  • tornado换curl_httpclient


  相关解决方案