Implementing a Simple Web Server to Run Scripts
Revision as of 19:14, 30 April 2008 by PeterHarding (talk | contribs)
Overview
Sometimes it is convenient to create a simple web server front-end to initiate the running of scripts via a web interface. The following is an example of a simple one based on SimpleHTTPModule...
This code makes use of the commands module (See Running Command-line Scripts in Python) to fire off a Python script which actually does the work - in this case a set of test ODBC accesses to an MS SqlServer database.
#!/usr/bin/env python """Simple HTTP Server. This module builds on BaseHTTPServer by implementing the standard GET and HEAD requests in a fairly straightforward manner. """ __version__ = "0.6" __all__ = ["SimpleHTTPRequestHandler"] import os import posixpath import BaseHTTPServer import urllib import urlparse import cgi import shutil import mimetypes import commands try: from cStringIO import StringIO except ImportError: from StringIO import StringIO class SimpleHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): """Simple HTTP request handler with GET and HEAD commands. This serves files from the current directory and any of its subdirectories. The MIME type for files is determined by calling the .guess_type() method. The GET and HEAD requests are identical except that the HEAD request omits the actual contents of the file. """ server_version = "SimpleHTTP/" + __version__ def do_GET(self): """Serve a GET request.""" #f = self.send_head() f = self.do_test() if f: self.copyfile(f, self.wfile) f.close() def do_HEAD(self): """Serve a HEAD request.""" f = self.send_head() if f: f.close() def send_head(self): pass def do_test(self): f = StringIO() f.write("<title>Test</title>\n") f.write("<h2>test</h2>\n") f.write("<hr>\n") (status, output) = commands.getstatusoutput("./test.py") lines = output.split('\n') for line in lines: f.write("%s<br>\n" % line) f.write("<hr>\n") length = f.tell() f.seek(0) self.send_response(200) self.send_header("Content-type", "text/html") self.send_header("Content-Length", str(length)) self.end_headers() return f def copyfile(self, source, outputfile): """Copy all data between two file objects. The SOURCE argument is a file object open for reading (or anything with a read() method) and the DESTINATION argument is a file object open for writing (or anything with a write() method). The only reason for overriding this would be to change the block size or perhaps to replace newlines by CRLF -- note however that this the default server uses this to copy binary data as well. """ shutil.copyfileobj(source, outputfile) def test(HandlerClass = SimpleHTTPRequestHandler, ServerClass = BaseHTTPServer.HTTPServer): BaseHTTPServer.test(HandlerClass, ServerClass) if __name__ == '__main__': test()
This code is a cut down version of the standard SimpleHTTPServer module from the 2.5.1 Python distribution (off cygwin) - found in /usr/lib/python2.5 directory.