from cStringIO import StringIO import BaseHTTPServer, SimpleHTTPServer from connexionsrv import handle_request class ConnexionHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): protocol_version = 'HTTP/1.1' def send_head(self): """Common code for GET and HEAD commands. This sends the response code and MIME headers. Return value is either a file object (which has to be copied to the outputfile by the caller unless the command was HEAD, and must be closed by the caller under all circumstances), or None, in which case the caller has nothing further to do. """ data, contenttype = handle_request(self.path) self.send_response(200) self.send_header("Content-Type", contenttype) self.send_header("Content-Length", len(data)) self.end_headers() return StringIO(data) def address_string(self): """Override to return the client address with no formatting.""" return "%s:%d" % self.client_address def version_string(self): return "custom" if __name__ == '__main__': import sys if sys.argv[1:]: port = int(sys.argv[1]) else: port = 8000 server_address = ('', port) httpd = BaseHTTPServer.HTTPServer(server_address, ConnexionHTTPRequestHandler) sa = httpd.socket.getsockname() print "Serving HTTP on", sa[0], "port", sa[1], "..." httpd.serve_forever()