Section 20.8. Web (HTTP) Servers


20.8. Web (HTTP) Servers

Until now, we have been discussing the use of Python in creating Web clients and performing tasks to aid Web servers in CGI request processing. We know (and saw earlier in Sections 20.2 and 20.3) that Python can be used to create both simple and complex Web clients. Complexity of CGI requests goes without saying.

However, we have yet to explore the creation of Web servers, and that is the focus of this section. If the Firefox, Mozilla, IE, Opera, Netscape, AOL, Safari, Camino, Epiphany, Galeon, and Lynx browsers are among the most popular Web clients, then what are the most common Web servers? They are Apache, Netscape, IIS, thttpd, Zeus, and Zope. In situations where these servers may be overkill for your desired application, Python can be used to create simple yet useful Web servers.

20.8.1. Creating Web Servers in Python

Since you have decided on building such an application, you will naturally be creating all the custom stuff, but all the base code you will need is already available in the Python Standard Library. To create a Web server, a base server and a "handler" are required.

The base (Web) server is a boilerplate item, a must have. Its role is to perform the necessary HTTP communication between client and server. The base server class is (appropriately) named HTTPServer and is found in the BaseHTTPServer module.

The handler is the piece of software that does the majority of the "Web serving." It processes the client request and returns the appropriate file, whether static or dynamically generated by CGI. The complexity of the handler determines the complexity of your Web server. The Python standard library provides three different handlers.

The most basic, plain, vanilla handler, named BaseHTTPRequestHandler, is found in the BaseHTTPServer module, along with the base Web server. Other than taking a client request, no other handling is implemented at all, so you have to do it all yourself, such as in our myhttpd.py server coming up.

The SimpleHTTPRequestHandler, available in the SimpleHTTP-Server module, builds on BaseHTTPRequestHandler by implementing the standard GET and HEAD requests in a fairly straightforward manner. Still nothing sexy, but it gets the simple jobs done.

Finally, we have the CGIHTTPRequestHandler, available in the CGIHTTPServer module, which takes the SimpleHTTPRequestHandler and adds support for POST requests. It has the ability to call CGI scripts to perform the requested processing and can send the generated HTML back to the client.

The three modules and their classes are summarized in Table 20.6.

Table 20.6. Web Server Modules and Classes

Module

Description

BaseHTTPServer

Provides the base Web server and base handler classes, HTTPServer and BaseHTTPRequestHandler, respectively

SimpleHTTPServer

Contains the SimpleHTTPRequestHandler class to perform GET and HEAD requests

CGIHTTPServer

Contains the CGIHTTPRequestHandler class to process POST requests and perform CGI execution


To be able to understand how the more advanced handlers found in the SimpleHTTPServer and CGIHTTPServer modules work, we will implement simple GET processing for a BaseHTTPRequestHandler.

In Example 20.9, we present the code for a fully working Web server, myhttpd.py.

Example 20.9. Simple Web Server (myhttpd.py)

This simple Web server can read GET requests, fetch a Web page (.html file) and return it to the calling client. It uses the BaseHTTPRequestHandler found in BaseHTTPServer and implements the do_GET() method to enable processing of GET requests.

  1   #!/usr/bin/env python   2   3     from os import curdir, sep   4     from BaseHTTPServer import \   5                 BaseHTTPRequestHandler, HTTPServer   6   7     class MyHandler(BaseHTTPRequestHandler):   8   9         def do_GET(self):   10            try:   11                f = open(curdir + sep + self.path)   12                self.send_response(200)   13                self.send_header('Content-type',   14                                 'text/html')   15                self.end_headers()   16                self.wfile.write(f.read())   17                f.close()   18            except IOError:   19                self.send_error(404,   20                    'File Not Found: %s' % self.path)   21   22   def main():   23       try:   24           server = HTTPServer(('', 80), MyHandler)   25           print 'Welcome to the machine...',   26           print 'Press ^C once or twice to quit.'   27           server.serve_forever()   28       except KeyboardInterrupt:   29           print '^C received, shutting down server'   30           server.socket.close()   31   32   if __name__ == '__main__':   33      main()

This server subclasses BaseHTTPRequestHandler and consists of a single do_GET() method, which is called when the base server receives a GET request. We attempt to open the path passed in by the client and if present, return an "OK" status (200) and forward the downloaded Web page. If the file was not found, it returns a 404 status.

The main() function simply instantiates our Web server class and invokes it to run our familiar infinite server loop, shutting it down if interrupted by ^C or similar keystroke. If you have appropriate access and can run this server, you will notice that it displays loggable output, which will look something like this:

# myhttpd.py Welcome to the machine... Press ^C once or twice to quit localhost - - [26/Aug/2000 03:01:35] "GET /index.html HTTP/1.0" 200 - localhost - - [26/Aug/2000 03:01:29] code 404, message File Not Found: /x.html localhost - - [26/Aug/2000 03:01:29] "GET /dummy.html HTTP/1.0" 404 - localhost - - [26/Aug/2000 03:02:03] "GET /hotlist.htm HTTP/1.0" 200 -


Of course, our simple little Web server is so simple, it cannot even process plain text files. We leave that as an exercise for the reader, which can be found at the end of the chapter.

As you can see, it doesn't take much to have a Web server up and running in pure Python. There is plenty more you can do to enhance the handlers to customize it to your specific application. Please review the Library Reference for more information on the modules (and their classes) discussed in this section.



Core Python Programming
Core Python Programming (2nd Edition)
ISBN: 0132269937
EAN: 2147483647
Year: 2004
Pages: 334
Authors: Wesley J Chun

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net