6.1 The request

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:

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):