Httpd.py

From PeformIQ Upgrade
Revision as of 18:37, 23 April 2009 by PeterHarding (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Script

#!/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.

"""

#---------------------------------------------------------------------

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

#---------------------------------------------------------------------

__version__ = "0.6"

__all__ = ["SimpleHTTPRequestHandler"]

#---------------------------------------------------------------------

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("ls -l")

        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)

#---------------------------------------------------------------------

def run(HandlerClass=SimpleHTTPRequestHandler,
         ServerClass=BaseHTTPServer.HTTPServer):

    server_address = ('', 9000)

    httpd = ServerClass(server_address, HandlerClass)

    httpd.serve_forever()

#---------------------------------------------------------------------

if __name__ == '__main__':
   run()

#---------------------------------------------------------------------