I l@ve RuBoard |
15.10 Rolling Your Own Servers in PythonMost of the Internet modules we looked at in the last few chapters deal with client-side interfaces such as FTP and POP, or special server-side protocols such as CGI that hide the underlying server itself. If you want to build servers in Python by hand, you can do so either manually or by using higher-level tools. 15.10.1 Coding SolutionsWe saw the sort of code needed to build servers manually in Chapter 10. Python programs typically implement servers either by using raw socket calls with threads, forks, or selects to handle clients in parallel, or by using the SocketServer module. In either case, to serve requests made in terms of higher-level protocols such as FTP, NNTP, and HTTP, you must listen on the protocol's port and add appropriate code to handle the protocol's message conventions. If you go this route, the client-side protocol modules in Python's standard library can help you understand the message conventions used. You may also be able to uncover protocol server examples in the Demos and Tools directories of the Python source distribution and on the Net at large (search http://www.python.org). See prior chapters for more details on writing socket-based servers. As a higher-level interface, Python also comes with precoded HTTP web protocol server implementations, in the form of three standard modules. BaseHTTPServer implements the server itself; this class is derived from the standard SocketServer.TCPServer class. SimpleHTTPServer and CGIHTTPServer implement standard handlers for incoming HTTP requests; the former handles simple web page file requests, while the latter also runs referenced CGI scripts on the server machine by forking processes. For example, to start a CGI-capable HTTP server, simply run Python code like that shown in Example 15-19 on the server machine. Example 15-19. PP2E\Internet\Other\webserver.py#!/usr/bin/python ############################################ # implement a HTTP server in Python which # knows how to run server-side CGI scripts; # change root dir for your server machine ############################################ import os from BaseHTTPServer import HTTPServer from CGIHTTPServer import CGIHTTPRequestHandler os.chdir("/home/httpd/html") # run in html root dir srvraddr = ("", 80) # my hostname, portnumber srvrobj = HTTPServer(srvraddr, CGIHTTPRequestHandler) srvrobj.serve_forever( ) # run as perpetual demon This assumes that you have appropriate permissions to run such a script, of course; see the Python library manual for more details on precoded HTTP server and request handler modules. Once you have your server running, you can access it in any web browser or by using either the Python httplib module, which implements the client side of the HTTP protocol, or the Python urllib module, which provides a file-like interface to data fetched from a named URL address (see the urllib examples in Chapter 11Chapter 11, and Chapter 13, and use a URL of the form "http://..." to access HTTP documents). 15.10.2 Packaged SolutionsFinally, you can deploy full-blown, open source, and Python-friendly web servers and tools that are freely available on the Net. These may change over time too, but here are a few current options:
Be sure to watch http://www.python.org for new developments on the server front, as well as late-breaking advances in Python web scripting techniques in general. |
I l@ve RuBoard |