pants.contrib.http.client

HTTPClient

class pants.contrib.http.client.HTTPClient(response_handler=None, max_redirects=5, keep_alive=True, unicode=True)[source]

An HTTP client, capable of communicating with most, if not all, servers using an incomplete implementation of HTTP protocol version 1.1.

The behavior of an instance of HTTPClient is determined by that instance’s on_response() function. That function may be changed by subclassing HTTPClient, assigning it directly, or supplying a suitable callable as the first argument when creating an instance of HTTPClient.

Argument Default Description
response_handler None Optional. A callable that will handle any received responses.
max_redirects 5 Optional. The number of times to follow a redirect issued by the server.
keep_alive True Optional. Whether or not a single connection will be reused for multiple requests.
unicode True Optional. Whether or not to attempt to convert the response body to unicode using the provided Content-Type header’s encoding information.
on_response(response)[source]

Placeholder. Called when an HTTP response is received.

Argument Description
response The received HTTP response.
get(url, timeout=30, headers=None, **kwargs)[source]

Perform an HTTP GET request for the specified URL. Additional query parameters may be specified as keyword arguments. For example:

client.get('http://www.google.com/search', q='test')

Is equivalent to:

client.get('http://www.google.com/search?q=test')
Argument Default Description
url   The URL to request.
timeout 30 Optional. The time, in seconds, to wait for a response before erroring.
headers None Optional. A dictionary of headers to send with the request. If none are provided, basic headers are set.
post(url, timeout=None, headers=None, files=None, **kwargs)[source]

Perform an HTTP POST request for the specified URL.

Argument Default Description
url   The URL to request.
timeout 30 Optional. The time, in seconds, to wait for a response before erroring.
headers None Optional. A dictionary of headers to send with the request. If none are provided, basic headers are set.
files None Optional. A dictionary of files to send with the request. If this is provided, the dictionary keys should be equivalent to HTML form field names, and the values should be tuples of (filename, data).

Any additional keyword arguments will be sent in the request body as POST variables.

process()[source]

Useful for testing, and other synchronous work, this function will start the Pants engine and block until all outstanding requests complete.

ClientHelper

class pants.contrib.http.client.ClientHelper(parent)[source]

An instance of this class is returned by calls to HTTPClient.get() and HTTPClient.post() to allow for a bit of decorator magic.

For further information, please see the HTTPClient documentation.

fetch()[source]

Perform all the pending requests and return them. If there is more than one outstanding request, the results will be returned as a list of HTTPResponse instances. Otherwise, a single HTTPResponse instance will be returned.

HTTPResponse

class pants.contrib.http.client.HTTPResponse(client, request, http_version, status, status_text, headers)[source]

Instances of this class represent singular HTTP responses that an instance of HTTPClient has received. Such instances contain all the information needed to act upon a response.

This class should, generally, not be used directly. Instead, allow the HTTPClient to create instances for you.

Argument Description
client The instance of HTTPClient that received this response.
request The request list, containing all the information passed to the call that generated the request responsible for this response.
http_version The HTTP protocol version used in this response. This will almost always be one of: HTTP/1.0 or HTTP/1.1.
status The HTTP status code received with this response.
status_text The human readable status message that goes with the received status code.
headers A dictionary of HTTP headers received with this response.
cookies[source]

An instance of Cookie.SimpleCookie representing the cookies received with this response.

full_url[source]

The full URL of the request that generated this response.

Table Of Contents

Previous topic

pants.contrib.http.server

Next topic

pants.contrib.wsgi