Tornado provides tornado.httpclient that works as httpclient and can send blocking and non-blocking (async) http requests.
tornado.httpclient.HTTPClient
1. HTTPClient: This tornado client is typically used for testing web servers or simply making a HTTP request and receiving the response. An example implementation below
print "Blocking HTTPClient" | |
from tornado import ioloop | |
from tornado import httpclient | |
http_client = httpclient.HTTPClient() | |
try: | |
http_client.fetch("http://google.co.in/") | |
print "HTTPClient Response" | |
except httpclient.HTTPError, e: | |
print "Error:", e |
tornado.httpclient.AsyncHTTPClient
2. AsyncHTTPClient: httpclient.AsyncHTTPClient() creates an instance of the class and that can be reused one per IO loop. Here’s an example implementation below
print "\nNon-Blocking AsyncHTTPClient" | |
import tornado.ioloop | |
def async_call(response): | |
if response.error: | |
response.rethrow() | |
print "AsyncHTTPClient Response" | |
ioloop.IOLoop.instance().stop() | |
http_client = httpclient.AsyncHTTPClient() | |
http_client.fetch("http://www.google.co.in/", async_call) | |
ioloop.IOLoop.instance().start() |