I'm a newbie to Tornado and I'm trying to use it to make an async HEAD HTTP request. How is that done with Tornado?
My question is inspired by this one: How do you send a HEAD HTTP request in Python 2?
I'm a newbie to Tornado and I'm trying to use it to make an async HEAD HTTP request. How is that done with Tornado?
My question is inspired by this one: How do you send a HEAD HTTP request in Python 2?
In Python 3.5+:
from tornado.httpclient import AsyncHTTPClient
from tornado.ioloop import IOLoop
async def fetch_head():
response = await http_client.fetch("http://www.google.com/")
print(response.headers)
IOLoop.current().stop()
http_client = AsyncHTTPClient()
IOLoop.current().add_callback(fetch_head)
IOLoop.current().start()
In real code, don't call "stop" until all processing is complete and your program is ready to exit.
In older Python replace async / await with gen.coroutine and yield:
from tornado import gen
@gen.coroutine
def fetch_head():
response = yield http_client.fetch("http://www.google.com/")
print(response.headers)
IOLoop.current().stop()
Add method="HEAD"
to your AsyncHTTPClient.fetch()
call.
response = await http_client.fetch("http://example.com", method="HEAD")