我读了Introduction to Tornado这本书。它在使用twitter搜索API的应用程序中介绍了tornado的异步特性。
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发出异步请求?谢谢。
发布于 2013-07-17 08:09:28
看一下随Tornado提供的TwitterMixin,并研究一下its code。
https://stackoverflow.com/questions/17668895
复制相似问题