#!/usr/bin/env python # # Basic HTTP Server giving access to the CCP4i database # Based on the basichttp.py example - in ""Foundations of Python Network # Programming" (John Goerzen) Chapter 16 # This server takes requests in the form of # http://localhost:8765/?command=...&project=...&job=... etc # It then connects to the handler, retrieves some data and sends # back a HTML page #CCP4i_cvs_Id $Id: http_dbclient.py,v 1.9 2008/08/12 10:48:13 pjx Exp $ from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler import sys import time import dbClientAPI class RequestHandler(BaseHTTPRequestHandler): def _writeheaders(self): self.send_response(200) self.send_header('Content-type','text/html') self.end_headers() def do_HEAD(self): self._writeheaders() def do_GET(self): self._writeheaders() # decode the input data = decodeRequest(self.path) print "Request decoded" # Execute the request result = dbccp4i_request(data) print "Sending result to requester" self.wfile.write("""
No projects available
" else: result += "" result += str(records[0][2])+": \""+str(records[0][3])+"\"" result += "
\n" jobs = db.ListJobs(project,data["job"]) subjobs = [] else: jobs = db.ListJobs(project) subjobs = db.ListJobswithsubjobs(project) if len(jobs) == 0: result += "There are currently no jobs in this project
\n" else: jobs.reverse() records = db.GetListofRecords(project,jobs,\ ["DATE","STATUS","TASKNAME","TITLE"]) result += """ID | DATE | STATUS | TASKNAME | TITLE | """ if not data.has_key("job"): result += "Subjobs |
---|---|---|---|---|---|
"+str(jobs[i])+" | \n" j = 0 for item in records[i]: if j == 0: # Convert the DATE to a human readable form item = dbClientAPI.ConvertTimeCCP4i(float(item)) j += 1 result += ""+str(item)+" | " # Indicate if there are subjobs available if not data.has_key("job"): try: subjobs.index(jobs[i]) result += "View subjobs | " except ValueError: result += "" result += " |
Unable to access the project
" # Links at the tail of the page # Refresh the view by executing the same request again result += "" result += " [Refresh]" # Link for subjobs to return to main job view if data.has_key("job"): result += " [List jobs in "+str(project)+"]" # Write a link to get back to the list of projects result += " [Back to projects]" result += "
\n" # Finish with the handler connection db.HandlerDisconnect() # Return the result print "Finished building result HTML" return result # Supporting functions def decodeRequest(url): """Break up a url into a set of arguments and values. Given a url of the form path?key1=value1&key2=value2&... return a dictionary where the keys are key1, key2 etc and the corresponding values are value1, value2 etc.""" request = url.split("?") try: args = request[1].split("&") except IndexError: args = request data= {} for arg in args: try: key = arg.split("=")[0] value = arg.split("=")[1] data[key] = value except: print "Error decoding "+arg return data def buildRequest(args): """Make a request string from a dictionary Given a dictionary of the form { 'key1':value1, 'key2':value2, ... } return a string of the form '?key1=value1&key2=value2&...""" request = [] for key in args: request.append(str(key)+"="+str(args[key])) request.sort() return "?"+"&".join(request) # Main program try: # Start the HTTP server port = 8765 serveraddr = ('', port) srvr = HTTPServer(serveraddr, RequestHandler) print "http_dbclient listening on port %d" % port print "Point your browser at http://localhost:%d to connect" % port print "Using Ctrl-C to stop the server" srvr.serve_forever() except KeyboardInterrupt: print "Finished" sys.exit(0)