annotate pythonServer.py @ 2087:3feebc3acec6

Add Graphics to DMRN Poster
author Dave Moffat <me@davemoffat.com>
date Wed, 16 Dec 2015 10:20:25 +0000
parents 56bdd4d357f9
children f9c9a40f33bd
rev   line source
me@2087 1 #!/usr/bin/python
me@2087 2
b@1551 3 import BaseHTTPServer
b@1551 4 from os import walk
b@1551 5 from os import path
nickjillings@1552 6 from os import listdir
b@2064 7 import inspect
b@2064 8 import os
b@1551 9 import urllib2
b@1551 10 import pickle
b@1551 11 import datetime
nickjillings@1542 12
b@2064 13 # Go to right folder.
b@2064 14 scriptdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) # script directory
b@2064 15 os.chdir(scriptdir) # does this work?
b@2064 16
nickjillings@1552 17 PSEUDO_PATH = 'example_eval/'
nickjillings@1552 18 pseudo_files = []
nickjillings@1552 19 for filename in listdir(PSEUDO_PATH):
nickjillings@1552 20 if filename.endswith('.xml'):
nickjillings@1552 21 pseudo_files.append(filename)
nickjillings@1552 22
b@1551 23 curSaveIndex = 0;
b@1551 24 curFileName = 'test-0.xml'
b@1551 25 while(path.isfile('saves/'+curFileName)):
b@1551 26 curSaveIndex += 1;
b@1551 27 curFileName = 'test-'+str(curSaveIndex)+'.xml'
nickjillings@1542 28
nickjillings@1552 29 print "Next save - " + curFileName
nickjillings@1552 30 pseudo_index = curSaveIndex % len(pseudo_files)
b@2063 31 print "Next test in pseudo-random queue - " + pseudo_files[pseudo_index]
nickjillings@1542 32
b@1551 33 def send404(s):
b@1551 34 s.send_response(404)
b@1551 35 s.send_header("Content-type", "text/html")
b@1551 36 s.end_headers()
b@1551 37
b@1551 38 def processFile(s):
b@1551 39 s.path = s.path[1:len(s.path)]
b@1551 40 st = s.path.rsplit(',')
b@1551 41 lenSt = len(st)
b@1551 42 fmt = st[lenSt-1].rsplit('.')
b@1551 43 size = path.getsize(urllib2.unquote(s.path))
b@1551 44 fileDump = open(urllib2.unquote(s.path))
b@1551 45 s.send_response(200)
b@1551 46
b@1551 47 if (fmt[1] == 'html'):
b@1551 48 s.send_header("Content-type", 'text/html')
b@1551 49 elif (fmt[1] == 'css'):
b@1551 50 s.send_header("Content-type", 'text/css')
b@1551 51 elif (fmt[1] == 'js'):
b@1551 52 s.send_header("Content-type", 'application/javascript')
b@1551 53 else:
b@1551 54 s.send_header("Content-type", 'application/octet-stream')
b@1551 55 s.send_header("Content-Length", size)
b@1551 56 s.end_headers()
b@1551 57 s.wfile.write(fileDump.read())
b@1551 58 fileDump.close()
b@1551 59
b@1551 60 def saveFile(self):
b@1551 61 global curFileName
b@1551 62 global curSaveIndex
b@1551 63 varLen = int(self.headers['Content-Length'])
b@1551 64 postVars = self.rfile.read(varLen)
b@1551 65 print curFileName
b@1551 66 file = open('saves/'+curFileName,'w')
b@1551 67 file.write(postVars)
b@1551 68 file.close()
nickjillings@1450 69 try:
nickjillings@1450 70 wbytes = os.path.getsize('saves/'+curFileName)
nickjillings@1450 71 except OSError:
nickjillings@1450 72 self.send_response(200)
nickjillings@1450 73 self.send_header("Content-type", "text/xml")
nickjillings@1450 74 self.end_headers()
nickjillings@1450 75 self.wfile.write('<response state="error"><message>Could not open file</message></response>')
b@1551 76 self.send_response(200)
b@1551 77 self.send_header("Content-type", "text/xml")
b@1551 78 self.end_headers()
nickjillings@1450 79 self.wfile.write('<response state="OK"><message>OK</message><file bytes="'+str(wbytes)+'">"saves/'+curFileName+'"</file></response>')
nickjillings@1450 80 curSaveIndex += 1
nickjillings@1450 81 curFileName = 'test-'+str(curSaveIndex)+'.xml'
nickjillings@1542 82
b@1551 83 class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
b@1551 84 def do_HEAD(s):
nickjillings@1552 85 s.send_response(200)
nickjillings@1552 86 s.send_header("Content-type", "text/html")
nickjillings@1552 87 s.end_headers()
b@1551 88 def do_GET(request):
nickjillings@1552 89 global pseudo_index
nickjillings@1552 90 global pseudo_files
nickjillings@1552 91 global PSEUDO_PATH
b@1551 92 if(request.client_address[0] == "127.0.0.1"):
b@1551 93 if (request.path == "/favicon.ico"):
b@1551 94 send404(request)
b@1551 95 else:
b@1551 96 if (request.path == '/'):
b@1551 97 request.path = '/index.html'
nickjillings@1552 98 elif (request.path == '/pseudo.xml'):
nickjillings@1552 99 request.path = '/'+PSEUDO_PATH + pseudo_files[pseudo_index]
nickjillings@1552 100 print request.path
nickjillings@1552 101 pseudo_index += 1
nickjillings@1552 102 pseudo_index %= len(pseudo_files)
b@1551 103 processFile(request)
b@1551 104 else:
b@1551 105 send404(request)
nickjillings@1552 106
b@1551 107 def do_POST(request):
b@1551 108 if(request.client_address[0] == "127.0.0.1"):
nickjillings@1450 109 if (request.path == "/save" or request.path == "/save.php"):
b@1551 110 saveFile(request)
b@1551 111 else:
b@1551 112 send404(request)
b@1551 113
b@1551 114 def run(server_class=BaseHTTPServer.HTTPServer,
b@1551 115 handler_class=MyHandler):
b@1551 116 server_address = ('', 8000)
b@1551 117 httpd = server_class(server_address, handler_class)
b@1551 118 httpd.serve_forever()
b@1551 119
b@1551 120 run()