1.3. A tiny Web client to play with

And, at last, we have to explore the client side. Instead of using telnet or a standard Web browser, we will write our own Web client. The two main tasks of a Web client are:

Exercise 1.9. The Web client code

import browser                                                            (1)
from httplib import HTTP                                                  (2)
import sys

from sys import argv
if len(argv) > 1:
if len(argv) > 2:
    port = '80'
if len(argv) > 3:
    document = argv[3]
    document = '/'

print host+':' + port + ' ' + document

def fetch(host, document, port=None):                                     (3)
    if port:
        req = HTTP(host + ':' + port)
    else:                                                                 (4)
        req = HTTP(host)
    req.putrequest("GET", document)                                       (5)

    req.putheader("Accept", "text/html")                                  (6)
    req.putheader("User-Agent", sys.argv[0])
    status_code, status_phrase, headers = req.getreply()                  (7)
    f=req.getfile()                                                       (8)
    f.close()                                                             (9)
    return result, str(status_code) + ' ' + status_phrase

text, status = fetch(host, document, port)
b = browser.Browser(fetch)
url = "http://" + host + ":" + port + document                            (10)
b.display(text, url, status)


browser is a just module defining a class to display text (see code/browser.py).


httplib is a module defining - among others things - a class (HTTP) to issue http requests.


A function to fetch document.


Instantiation of HTTP class: this creates a client, or an agent to issue requests.


Put the request.


Write headers.


Actually send the request with headers.


Print HTTP status code and message.


Get the actual requested document.


Display it in the browser. Note that the fetch function is passed to the browser at instantiation.

Exercise 1.10. Use the Web client

Use the client script defined in Exercise 1.9 to request the server defined in Exercise 1.6:

% python -i tiny_client.py feu.sis.pasteur.fr 2122 tralala
Then Use this client script to get the home page of the Pasteur Institute Web server: what happens?
% python -i tiny_client.py www.pasteur.fr 
Issue a more precise request, such as:
%  python -i tiny_client.py www.pasteur.fr 80 /formation/infobio/
Is that better? It seems OK, but it is still non-handled HTML. So, what about asking for a non-HTML document:
%  python -i tiny_client.py www.pasteur.fr 80 /formation/infobio/2003/python/solutions/sol_code/fasta_fetcher.py

Exercise 1.11. Enhance the Web client

Add a field displaying the date of the fetched document to the browser (defined in Exercise 1.9). The date of the document lies in the last-modified header. For this purpose, you will have to:

  • add a Label field to the browser,
  • change the fetch function to return an additional information (the last-modified header); for instance, use the dir() to look at the returned headers object in order to find out this header.