我读过这本书Introduction to Tornado。它使用twitter搜索API在应用程序中引入了龙卷风的异步功能。
code如下:
class IndexHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
@tornado.gen.engine
def get(self):
query = self.get_argument('q')
client = tornado.httpclient.AsyncHTTPClient()
response = yield tornado.gen.Task(client.fetch,
"http://search.twitter.com/search.json?" + \
urllib.urlencode({"q": query, "result_type": "recent", "rpp": 100}))
...
self.finish()
它使用v1 twitter API搜索关键字。但是,新的v1.1 twitter API禁止使用非oauth请求。因此,我必须使用oauth库和我的消费者密钥和访问密钥来请求twitter搜索API。
def request_twitter(url, http_method = 'GET', post_body = '', http_headers = ''):
consumer = oauth.Consumer(key = consumer_key, secret = consumer_secret)
token = oauth.Token(key = access_token, secret = access_secret)
client = oauth.Client(consumer, token)
request = client.request(url, method = http_method, body = post_body, headers = http_headers)
return request
但oauth客户端不提供异步请求方式。所以我想知道如何在python中使用oauth客户端向twitter API发出异步请求?感谢。