这是我的代码
class MainHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def get(self):
http_client = tornado.httpclient.AsyncHTTPClient()
http_client.fetch("http://www.example.com",
callback=self.on_fetch)
def on_fetch(self, response):
self.write('hello')
self.finish()
Run Code Online (Sandbox Code Playgroud)
我想使用异步HTTP客户端.当我获取请求时,我想用cookie发送它.
该文档没有关于httpclient cookie的任何信息.http://tornado.readthedocs.org/en/latest/httpclient.html
您可以将cookie放在需要的headers关键字参数中fetch.
客户:
import tornado.httpclient
http_client = tornado.httpclient.HTTPClient()
cookie = {"Cookie" : 'my_cookie=heyhey'}
http_client.fetch("http://localhost:8888/cook",
headers=cookie)
Run Code Online (Sandbox Code Playgroud)
服务器:
from tornado.ioloop import IOLoop
import tornado.web
class CookHandler(tornado.web.RequestHandler):
def get(self):
cookie = self.get_cookie("my_cookie")
print "got cookie %s" % (cookie,)
if __name__ == "__main__":
app = tornado.web.Application([
(r"/cook", CookHandler),
])
app.listen(8888)
IOLoop.instance().start()
Run Code Online (Sandbox Code Playgroud)
如果您运行服务器,然后是客户端,服务器将输出:
got cookie heyhey
Run Code Online (Sandbox Code Playgroud)