CDT*_*CDT 2 python proxy tornado
我想从终端使用Twitter访问Twitter tornado.httpcilent.
但Twitter在我的国家受到了防火墙的攻击.我如何通过代理访问它?
还有其他选择吗?
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)
| 归档时间: |
|
| 查看次数: |
2039 次 |
| 最近记录: |