1.2. A tiny Web server to play with

Now that we have played with the HTTP protocol, e.g by issuing requests and specifying HTTP headers, let us see how a Web server work by programming a small server, that is only able to handle GET requests.

Exercise 1.6. The Web server code

	      
from BaseHTTPServer import HTTPServer
from BaseHTTPServer import BaseHTTPRequestHandler

class myHandler(BaseHTTPRequestHandler):
    
    def do_GET(self):
        self.printCustomHTTPResponse(200)                                 (1)
        self.wfile.write("<html>\n<body>\n")
        self.wfile.write("<p>GET string: " + self.path + "</p>" )
        self.printBrowserHeaders()
        self.wfile.write("</body>\n</html>\n")

    def printBrowserHeaders(self):
        keys = self.headers.dict.keys()                                   (2)
        self.wfile.write("\n<ul>")
        for key in keys:
            self.wfile.write("\n<li><b>" + key + "</b>: ")
            self.wfile.write(self.headers.dict[key] + "\n</li>\n")
        self.wfile.write("</ul>\n")

    def printCustomHTTPResponse(self, respcode):
        self.send_response(respcode)
        self.send_header("Content-Type", "text/html")                     (3)
        self.send_header("Server", "myHandler")
        self.end_headers()

    def log_request(self, code='-', size='-'):
        user_agent = self.headers.dict['user-agent']
        self.log_message('"%s" %s %s %s',
                         self.requestline, str(code), str(size), user_agent)

if __name__ == "__main__":
    server = HTTPServer(('',2122), myHandler)                             (4)
    for lp in range(5000):
        server.handle_request()


    
      
	    
1

This is the method that is called when a GET is issued.

2

This method displays the headers that have been set by the client.

3

This method set the server response headers.

4

Run the server: provide the myHandler class as your custom requests handler, and tell that you want to listen on port 2122.

We want to run this server of course. From the Unix prompt, run:

% python tiny_server.py
	  
and let the program wait for client requests (requests will be displayed on the terminal).

Exercise 1.7. Using our server

Now, we can request our server (defined and run as shown in Exercise 1.6) with ... a telnet interactive session as in the first exercises of this practical session.

% telnet feu.sis.pasteur.fr 2122
Trying 157.99.60.151...
Connected to feu.sis.pasteur.fr.
Escape character is '^]'.
GET tralalaitou HTTP/1.0
User-Agent: telnet + my fingers 
Connection: keep-alive

HTTP/1.0 200 OK
Server: BaseHTTP/0.2 Python/2.2
Date: Tue, 18 Feb 2003 17:15:49 GMT
Content-Type: text/html
Server: myHandler

<html>
<body>
<p>GET string: tralalaitou</p>
<ul>
<li><b>connection</b>: keep-alive
</li>
<li><b>user-agent</b>: telnet + my fingers 
</li>
</ul>
</body>
</html>
Connection closed by foreign host.
%
	  

Exercise 1.8. Using our server with a standard Web browser

Now, we can of course also request our simplistic server with a proper Web browser. First check that the server is running, and launch a browser on the following url: http://feu.sis.pasteur.fr:2122/tralalilaeere.