问题
I want to access Twitter from the terminal using tornado.httpcilent
.
But Twitter is firewalled in my country. How can I access it through proxy?
Are there any other options?
回答1:
The official documentation for tornado.httpclient contains examples how to use proxy.
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
Then try this code:
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()
来源:https://stackoverflow.com/questions/12016951/accessing-twitter-using-tornado-httpclient-using-proxy