Requests

This module implements functions to easily handle the intricacies of the HTTP protocol. The name and the API are inspired by the wonderful Python module Requests. To use requests a net driver must have been properly configured and started.

NOTE: at the moment, only the verb GET is supported

get(url, params=None, headers=None, connection=None)

Implements the GET method of the HTTP protocol. A tcp connection is made to the host:port given in the url using the default net driver.

If params is given as a dictionary, each pair (key, value) is appended to the requested url, properly encoded and sent.

If headers is given as a dictionary, each pair (key, value) is appropriately sent as a HTTP request header. Mandatory headers are transparently handled: “Host:” is always derived by parsing url; other headers are set to defaults if not given: for example “Connection: close” is sent if no value for “Connection” is specified in headers. To request a permanent connection, headers must contain the pair {“Connection”:”Keep-Alive”}.

If connection is given, the initial connection step is skipped and connection is used for communication. This feature allows the reuse of a connection to a HTTP server opened with a “Keep-Alive” header.

get returns a Response instance.

Exceptions can be raised: HTTPConnectionError when the HTTP server can’t be contacted; IOError when the source of error lies at the socket level (i.e. closed sockets, invalid sockets, etc..)

class Response

This class represent the result of a HTTP request.

It contains the following members:

status

Contains the HTTP response code

content

It is the bytearray containing the byte version of the content section of a HTTP response

headers

A dictionary with all the response headers

connection

the connection used to communicate with the server, or None if it has been closed.

text()

Returns a string representing the content section of the HTTP response