annotate pythonServer.py @ 278:8020152a36af

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