使用tornado.httpclient使用代理访问twitter

CDT*_*CDT 2 python proxy tornado

我想从终端使用Twitter访问Twitter tornado.httpcilent.

但Twitter在我的国家受到了防火墙的攻击.我如何通过代理访问它?

还有其他选择吗?

Iho*_*nko 5

tornado.httpclient官方文档包含如何使用代理的示例.

您需要curl后端才能获得代理支持.所以安装先决条件.以下是如何为Ubuntu做到这一点:

$ sudo apt-get install libcurl-dev librtmp-dev 
$ pip install tornado pycurl
Run Code Online (Sandbox Code Playgroud)

然后试试这段代码:

from tornado import httpclient, ioloop

config = {
    'proxy_host': 'YOUR_PROXY_HOSTNAME_OR_IP_ADDRESS',
    'proxy_port': 3128
}

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

def handle_request(response):
    if response.error:
        print "Error:", response.error
    else:
        print response.body
    ioloop.IOLoop.instance().stop()

http_client = httpclient.AsyncHTTPClient()
http_client.fetch("http://twitter.com/",
    handle_request, **config)
ioloop.IOLoop.instance().start()
Run Code Online (Sandbox Code Playgroud)

  • 应该警告,因为这个损失了2个小时.Tornado仅支持使用Pycurl的代理.Pycurl仅支持2.x Python分支.所以,没有Python 3.x的代理 (2认同)