#!/usr/bin/env python
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
import tornado.httpclient
import urllib
import json
import datetime
import time
from tornado.options import define, options
define("port", default=8000, help="run on the given port", type=int)
config = {
'proxy_host': '58.59.21.228',
'proxy_port': 25,
'proxy_username': 'yz',
'proxy_password': 'fangbinxingqusi',
}
def handle_request(response):
if response.error:
print "Error:", response.error
tornado.httpclient.AsyncHTTPClient.configure("tornado.curl_httpclient.CurlAsyncHTTPClient")
class IndexHandler(tornado.web.RequestHandler):
def get(self):
client = tornado.httpclient.AsyncHTTPClient()
response = client.fetch("http://twitter.com/", handle_request,
**config)
self.write(response.body)
if __name__ == "__main__":
tornado.options.parse_command_line()
app = tornado.web.Application(handlers=[(r"/", IndexHandler)],
debug=True)
httpserver = tornado.httpserver.HTTPServer(app)
httpserver.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
在运行时,方法response
中的对象get()
被报告为一种None
类型。如何在fetch()
方法中获得get()
方法的响应?