A very common task when programming web services is to create web servers to handle special processing of GET requests from web browsers. The BaseHTTPServer module included with Python provides a set of classes and functions that allow you to create custom web servers to handle these requests. The first step is to define a handler class derived from the BaseHTTPServer.BaseHTTPRequestHandler class that overrides the do_GET() method. Inside the do_GET method, you can use the path attribute to get the file path the GET request was directed toward. The path attribute includes the entire string of the GET request, including the path and parameters in the format path?param=value¶m=value.... If there were parameters passed in the GET request, they can be parsed out by using the split('?') function on the path string to split it into a path and query string, as illustrated by the sample code http_get_serv.py. When you have the query string of the POST request in a buffer, use cgi.parse_qsl(string) to parse the query string into a dictionary, as shown in the example http_get_serv.py. The arguments will be added to the dictionary and can be accessed by using standard Python syntax. Note In the sample code, we are using the web server to remotely execute a Python script. We redirect the sys.stdout to the wfile attribute of the handler class so that normal output from the script executing will be displayed in the web browser. Once you have defined the handler class and overridden the do_GET method, create an instance of the web server using BaseHTTPServer.HTTPServer(address, handler). The address argument is a list including the server address and port, respectively. The handler argument is the custom handler class you defined earlier. After you have created an instance of the web server, start the web server by calling its serve_forever() function. import os, sys import BaseHTTPServer, cgi servAddr = ('',8080) #Define the HTTP handler that overrides do_GET class httpServHandler( \ BaseHTTPServer.BaseHTTPRequestHandler): def do_GET(self): if self.path.find('?') != -1: self.path, self.query_string = \ self.path.split('?', 1) else: self.query_string = '' self.send_response(200) self.send_header('Content-type', 'text/html') self.end_headers() #Setup Global Environment self.globals = \ dict(cgi.parse_qsl(self.query_string)) #Redirect output to browser sys.stdout = self.wfile #Execute the script remotely self.wfile.write("<h2>Handle Get</h2><P>") self.wfile.write( "<LI>Executing <b>%s</b>" % (self.path)) self.wfile.write( \ "<li>With Globals<B>%s</b><hr>" % \ (self.globals)) execfile(self.path, self.globals) #Set the root directory os.chdir('/myTest') #Create server object serv = BaseHTTPServer.HTTPServer( \ servAddr, httpServHandler) #Start Server serv.serve_forever() http_get_serv.py if name and quote: print "<B>%s</B> says <I>%s</I>"% (name, quote) else: print "There were errors in the parameters." http_text.py Figure 10.7 shows the web page generated by http_get_serv.py when it receives a GET request. Figure 10.7. Output HTML page created by http_get_serv.py code. |