本文介绍了使用 tornado.httpclient 使用代理访问 twitter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to access Twitter from the terminal using tornado.httpcilent.

但 Twitter 在我的国家/地区受到防火墙限制.我如何通过代理访问它?

But Twitter is firewalled in my country. How can I access it through proxy?

还有其他选择吗?

推荐答案

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

The official documentation for tornado.httpclient contains examples how to use proxy.

您将需要 curl 后端以获得代理支持.所以安装先决条件.以下是针对 Ubuntu 执行此操作的方法:

You will need curl backend for proxy support. So install the prerequisites. Here's how to do that for Ubuntu:

$ sudo apt-get install libcurl-dev librtmp-dev 
$ pip install tornado pycurl

然后试试这个代码:

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()

这篇关于使用 tornado.httpclient 使用代理访问 twitter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 20:07