Skip to content

Instantly share code, notes, and snippets.

/dispatcher.py
Created Feb 8, 2010

Embed
What would you like to do?
import string,cgi,time,psycopg2,uuid
from os import curdir, sep
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
class MyHandler(BaseHTTPRequestHandler):
def do_GET(self):
i = 0
maxsize = 0
timeout = 0
jobid = uuid.uuid4()
try:
if self.path.endswith("dojo.xd.js") or self.path.endswith("Chemene.JSDraw.js") or self.path.endswith("robots.txt"):
f = open("." + sep + self.path)
self.wfile.write(f.read())
f.close()
if self.path.endswith("get"):
uatype = self.headers.getheader('user-agent')
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
if (uatype.find('Firefox/3') != -1):
timeout = 500
maxsize = 25
elif (uatype.find('Chrome/3') != -1):
timeout = 200
maxsize = 100
elif (uatype.find('MSIE') != -1):
maxsize = 5
timeout = 1000
else:
return
try:
conn = psycopg2.connect("dbname='' user='' host='localhost' password=''");
cur = conn.cursor()
cur.execute("""SELECT id,molfile(structure) FROM <table> ORDER BY random() LIMIT %d""" % maxsize)
rows = cur.fetchall()
cur.close()
conn.close()
except:
print "cannot connect to database diss"
self.wfile.write("<html>\r\n")
self.wfile.write("<head>\r\n")
self.wfile.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\r\n")
self.wfile.write("<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">\r\n")
self.wfile.write("<script type=\"text/javascript\" src=\"dojo.xd.js\"></script> \r\n")
self.wfile.write("<script type=\"text/javascript\" src=\"Chemene.JSDraw.js\"></script>\r\n")
self.wfile.write("<title>Now running job %s</title>\r\n" % jobid)
self.wfile.write("</head>\r\n")
self.wfile.write("<body onload=\"JSDraw.init();dsss();\">\r\n")
self.wfile.write("Now running job %s\r\n<br>" % jobid);
self.wfile.write("Job size: %d \r\n<br>" % len(rows));
self.wfile.write("<div class=\"JSDraw\" id=\"q\" style=\"visibility: hidden\" viewonly\r\n")
self.wfile.write(" molfile='\r\n")
self.wfile.write("ChemJSDraw11140919362D\r\n")
self.wfile.write("\r\n")
self.wfile.write(" 6 6 0 0 0 0 0 0 0 0 1 V2000\r\n")
self.wfile.write(" 3.5115 -9.3600 0.0000 C 0 0\r\n")
self.wfile.write(" 2.1605 -8.5800 0.0000 C 0 0\r\n")
self.wfile.write(" 2.1605 -7.0200 0.0000 C 0 0\r\n")
self.wfile.write(" 3.5115 -6.2400 0.0000 N 0 0\r\n")
self.wfile.write(" 4.8625 -7.0200 0.0000 C 0 0\r\n")
self.wfile.write(" 4.8625 -8.5800 0.0000 C 0 0\r\n")
self.wfile.write(" 1 2 1 0\r\n")
self.wfile.write(" 2 3 2 0\r\n")
self.wfile.write(" 3 4 1 0\r\n")
self.wfile.write(" 4 5 2 0\r\n")
self.wfile.write(" 5 6 1 0\r\n")
self.wfile.write(" 6 1 2 0\r\n")
self.wfile.write("M END'></div>\r\n")
self.wfile.write("\r\n")
for row in rows:
self.wfile.write("<div class=\"JSDraw\" id=\"t%d\" style=\"visibility: hidden\" viewonly\r\n extid=\"%d\" molfile='" % (i,row[0]))
self.wfile.write(row[1])
i+=1
self.wfile.write("'></div>\r\n")
self.wfile.write("\r\n")
self.wfile.write("<form id=\"rF\" name=\"rF\" method=\"post\" action=\"return\">\r\n")
self.wfile.write("<input type=\"hidden\" id=\"jobid\" name=\"jobid\" value=\"%s\"></input>\r\n" % jobid)
self.wfile.write("<input type=\"hidden\" id=\"dur\" name=\"dur\" value=\"\"></input>\r\n")
self.wfile.write("<input type=\"hidden\" id=\"mr\" name=\"mr\" value=\"\"></input>\r\n")
self.wfile.write("</form>\r\n")
self.wfile.write("\r\n")
self.wfile.write("<script type=\"text/javascript\">\r\n")
self.wfile.write(" function dsss() {\r\n")
self.wfile.write(" var result = '';\r\n")
self.wfile.write(" var start = new Date().getTime()\r\n");
for j in range (0,i):
self.wfile.write(" if(JSDraw.get('q').sss(JSDraw.get('t%d'))) {\r\n" % j)
self.wfile.write(" result = result + '|' + document.getElementById('t%d').getAttribute('extid');\r\n" % j)
self.wfile.write(" }\r\n")
self.wfile.write(" document.getElementById('dur').value=new Date().getTime() - start;\r\n")
self.wfile.write(" document.getElementById('mr').value=result;\r\n")
self.wfile.write(" document.forms[0].submit();\r\n")
self.wfile.write(" }\r\n")
self.wfile.write("setTimeout(\"dsss()\",%d);\r\n" % timeout)
self.wfile.write("</script>\r\n")
self.wfile.write("\r\n")
self.wfile.write("</body>\r\n")
self.wfile.write("</html>\r\n")
self.wfile.write("\r\n")
return
except IOError:
self.send_error(404,'File Not Found: %s' % self.path)
def do_POST(self):
#global rootnode
try:
if self.path.endswith("return"):
ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
length = int(self.headers.getheader('content-length'))
self.send_response(200)
self.end_headers()
query=cgi.parse_qs(self.rfile.read(length))
ji = query.get('jobid')
if ji:
print ji[0]
du = query.get('dur')
if du:
print du[0]
mr = query.get('mr')
if mr:
lr = mr[0].lstrip('|')
hits = lr.split('|')
print hits
self.wfile.write("<html>\r\n")
self.wfile.write("<head>\r\n")
self.wfile.write("<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">\r\n")
self.wfile.write("<meta http-equiv=\"refresh\" content=\"5; URL=get\">\r\n")
self.wfile.write("<title>Thank you for running job %s</title>\r\n" % ji[0])
self.wfile.write("</head>\r\n")
self.wfile.write("<body>\r\n")
self.wfile.write("Thank you for running job %s<br>\r\n" % ji[0])
self.wfile.write("Total execution time: %s milliseconds<br>\r\n" % du[0])
self.wfile.write("Retrieving next job in %d second(s)\r\n" % 5)
self.wfile.write("</body>\r\n")
self.wfile.write("</html>\r\n")
return
except :
pass
def main():
try:
server = HTTPServer(('', 8080), MyHandler)
print 'started job dispatch server...'
server.serve_forever()
except KeyboardInterrupt:
print '^C received, shutting down server'
server.socket.close()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.