A client, like a web browser, sends an HTTP request to the server. An HTTP request looks like this:
POST /;login HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0
Accept: text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: es,en;q=0.8,fr;q=0.5,pt;q=0.3
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: http://localhost:8080/;login_form
Content-Type: application/x-www-form-urlencoded
Content-Length: 34
username=toto%40xxx.com&password=a
As you see it is basically a file, which is sent through the wire, from the client to the server. It has a simple readable format, which consists of three parts:
The request line. It is the first line, and contains: the request method, the requested uri and the protocol version.
The headers. Each line after the request line, and before the first blank line, is a header; where a header has a name and a value. Headers are used to information about the client (User-Agent, Agent), about the connection (Keep-Alive, Connection), etc.
The body. At the end, separated from the header by a blank line, comes the body. It contains extra information, for example the values of a form, or a file that is uploaded. Most requests do not include a body.
To work with HTTP requests (and responses) itools.http offers a file handler:
>>> from itools.http import Request
>>>
>>> request = Request('request.txt')
>>> request
<itools.http.request.Request object at 0x2b59beffc6d8>
>>>
>>> print request.method
POST
>>> print request.request_uri
/;login
>>> print request.http_version
HTTP/1.1
>>> print request.get_header('user-agent')
Mozilla/5.0
Here there is a summary of the programming interface of request objects (the details can be found in the reference chapter):
set_header(name, value)
has_header(name)
get_header(name)
get_referrer()
get_parameter(name, default=None, type=None))
has_parameter(name)
set_cookie(name, value)
get_cookie(name)