Python httpd
Jump to navigation
Jump to search
httpd.py
This script is set up to listen on port 8000 and run test.py when you do a request.
#!/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()
#-------------------------------------------------------------------------------
test.py
This is the test program which uses the ODBC connection and does the work. I also uses the ASctiveState ODBC as I did not manage to find a way of brining up ODBC in the time I had available using the native Cygwin Python.
$ cat test.py
#!/c/PROGRA~1/ActiveState/Python/python.exe
#
#-------------------------------------------------------------------------------
import os
import sys
import dbi
import odbc
import time
import getopt
import logging
from datetime import datetime
#-------------------------------------------------------------------------------
from dserver import client
#-------------------------------------------------------------------------------
__version__ = "1.0.0"
__id__ = "@(#) test.py [%s] 05/03/2008"
verbose_flg = False
debug_level = 0
table_name = "Manifests"
PORT = 9579
MAX_REQUESTS = 10
log = None
handle = 'ODBC_%02d'
connector_no = 1
#===============================================================================
def INFO(msg):
if log: log.info(' ' + msg)
if verbose_flg: print "[test] %s" % msg
#-------------------------------------------------------------------------------
def ERROR(msg):
if log: log.error(msg)
sys.stderr.write('[test] %s\n' % msg)
#-------------------------------------------------------------------------------
def WARNING(msg):
if log: log.warning('*****' + msg + '*****')
if verbose_flg: print "[test] %s" % msg
#===============================================================================
def elapsed_time():
t = float(ref_time(False)) * 0.001
return t
#------------------------------------------------------------------------------
t_reference = None
def ref_time(flg):
global t_reference
t_now = datetime.now()
if (flg):
t_reference = t_now
t = 0
else:
t_delta = t_now - t_reference
t = ((t_delta.seconds * 1000000) + t_delta.microseconds)/1000.0
return t
#===============================================================================
def query(no_queries, connector_no):
ds = client.Connect(port=PORT)
if (ds == None):
print("Connection to data server failed - is data server process running?\n")
return 1
type_ref = ds.RegisterType(table_name)
if debug_level > 0:print "Data type \"%s\" registered as %d" % (table_name, type_ref)
s = odbc.odbc(handle % connector_no)
cur = s.cursor()
cnt = 0
while cnt < no_queries:
cnt += 1
sp = ds.GetNext(type_ref)
if sp != None:
manifest = sp[0]
else:
print "Type %d exhausted" % (pid, type_ref)
return
# print manifest
qry = "exec procedure %s" % manifest
# print qry
ref_time(True)
cur.execute(qry)
t_query = elapsed_time()
# print cur.description
# for tup in cur.description:
# print tup[0]
row_cnt = 0
while 1:
rec = cur.fetchmany(10)
row_cnt += len(rec)
if not rec: break
#print rec
msg = "%02d ManifestNo %s [%.3f] sec %3d rows returned" % (cnt, manifest, t_query, row_cnt)
sys.stderr.write("%s\n" % msg)
sys.stderr.flush()
INFO(msg)
#-------------------------------------------------------------------------------
def init():
global lf
global log
pid = os.getpid()
if debug_level > 0: print "My PID is %d" % pid
log = logging.getLogger('test')
hdlr = logging.FileHandler('log/hcodevj86_%02d.log' % connector_no)
fmtr = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
hdlr.setFormatter(fmtr)
log.addHandler(hdlr)
log.setLevel(logging.INFO)
INFO("===== Started processing ==================================")
#===============================================================================
def main():
global verbose_flg
global debug_level
global connector_no
global MAX_REQUESTS
try:
opts, args = getopt.getopt(sys.argv[1:], "c:dD:M:vVw?")
except getopt.error, msg:
print __doc__
return 1
for o, a in opts:
if o == '-?':
print __doc__
return 1
elif o == '-c':
connector_no = int(a)
elif o == '-d':
debug_level += 1
elif o == '-D':
debug_level = int(a)
elif o == '-M':
MAX_REQUESTS = int(a)
elif o == '-v':
verbose_flg = True
elif o == '-V':
print "[xxxx] Version: %s" % __version__
return 1
else:
print __doc__
return 1
sys.stderr.write("[test] Working directory is %s - Using ODBC connector %d\n" % (os.getcwd(), connector_no))
sys.stderr.flush()
if (debug_level > 0): print "Debugging level set to %d" % debug_level
if args:
for arg in args:
print arg
init()
query(MAX_REQUESTS, connector_no)
return 0
#-------------------------------------------------------------------------------
if __name__ == '__main__' or __name__ == sys.argv[0]:
try:
sys.exit(main())
except KeyboardInterrupt, e:
print "[xxxx] Interrupted!"
#-------------------------------------------------------------------------------
"""
Revision History:
Date Who Description
-------- --- ------------------------------------------------------------
20031014 plh Initial implementation
Problems to fix:
To Do:
Issues:
"""