Blocking and non-blocking HTTP client interfaces.
This module defines a common interface shared by two implementations, simple_httpclient and curl_httpclient. Applications may either instantiate their chosen implementation class directly or use the AsyncHTTPClient class from this module, which selects an implementation that can be overridden with the AsyncHTTPClient.configure method.
The default implementation is simple_httpclient, and this is expected to be suitable for most users’ needs. However, some applications may wish to switch to curl_httpclient for reasons such as the following:
Note that if you are using curl_httpclient, it is highly recommended that you use a recent version of libcurl and pycurl. Currently the minimum supported version is 7.18.2, and the recommended version is 7.21.1 or newer.
A blocking HTTP client.
This interface is provided for convenience and testing; most applications that are running an IOLoop will want to use AsyncHTTPClient instead. Typical usage looks like this:
http_client = httpclient.HTTPClient() try: response = http_client.fetch("http://www.google.com/") print response.body except httpclient.HTTPError, e: print "Error:", e
Closes the HTTPClient, freeing any resources used.
Executes a request, returning an HTTPResponse.
If an error occurs during the fetch, we raise an HTTPError.
An non-blocking HTTP client.
import ioloop def handle_request(response): if response.error: print "Error:", response.error else: print response.body ioloop.IOLoop.instance().stop() http_client = httpclient.AsyncHTTPClient() http_client.fetch("http://www.google.com/", handle_request) ioloop.IOLoop.instance().start()
The constructor for this class is magic in several respects: It actually creates an instance of an implementation-specific subclass, and instances are reused as a kind of pseudo-singleton (one per IOLoop). The keyword argument force_instance=True can be used to suppress this singleton behavior. Constructor arguments other than io_loop and force_instance are deprecated. The implementation subclass as well as arguments to its constructor can be set with the static method configure()
Destroys this http client, freeing any file descriptors used. Not needed in normal use, but may be helpful in unittests that create and destroy http clients. No other methods may be called on the AsyncHTTPClient after close().
Executes a request, calling callback with an HTTPResponse.
If an error occurs during the fetch, the HTTPResponse given to the callback has a non-None error attribute that contains the exception encountered during the request. You can call response.rethrow() to throw the exception (if any) in the callback.
Configures the AsyncHTTPClient subclass to use.
AsyncHTTPClient() actually creates an instance of a subclass. This method may be called with either a class object or the fully-qualified name of such a class (or None to use the default, SimpleAsyncHTTPClient)
If additional keyword arguments are given, they will be passed to the constructor of each subclass instance created. The keyword argument max_clients determines the maximum number of simultaneous fetch() operations that can execute in parallel on each IOLoop. Additional arguments may be supported depending on the implementation class in use.
HTTP client request object.
Creates an HTTPRequest.
All parameters except url are optional.
HTTP Response object.
request: HTTPRequest object
code: numeric HTTP status code, e.g. 200 or 404
headers: httputil.HTTPHeaders object
buffer: cStringIO object for response body
body: respose body as string (created on demand from self.buffer)
error: Exception object, if any
request_time: seconds from request start to finish
Available data are subject to change, but currently uses timings available from http://curl.haxx.se/libcurl/c/curl_easy_getinfo.html, plus ‘queue’, which is the delay (if any) introduced by waiting for a slot under AsyncHTTPClient’s max_clients setting.
Exception thrown for an unsuccessful HTTP request.
response - HTTPResponse object, if any.
Note that if follow_redirects is False, redirects become HTTPErrors, and you can look at error.response.headers[‘Location’] to see the destination of the redirect.
This module provides a simple command-line interface to fetch a url using Tornado’s HTTP client. Example usage:
# Fetch the url and print its body python -m tornado.httpclient http://www.google.com # Just print the headers python -m tornado.httpclient --print_headers --print_body=false http://www.google.com