annotate pythonServer.py @ 882:594e617b75ad

PythonServer now supports pseudorandom page setup using preconfigured XML files. Set the PSEUDO_PATH in the python file, it will cycle through all .xml files in there. Index based off file save number. Set the .html url to /pseudo.xml to trigger
author Nicholas Jillings <nicholas.jillings@eecs.qmul.ac.uk>
date Sun, 28 Jun 2015 10:33:47 +0100
parents 2580a999670f
children a2800d0b5656 b7fd0296c6ab
rev   line source
BrechtDeMan@881 1 import BaseHTTPServer
BrechtDeMan@881 2 from os import walk
BrechtDeMan@881 3 from os import path
nicholas@882 4 from os import listdir
BrechtDeMan@881 5 import urllib2
BrechtDeMan@881 6 import pickle
BrechtDeMan@881 7 import datetime
nicholas@872 8
nicholas@882 9 PSEUDO_PATH = 'example_eval/'
nicholas@882 10 pseudo_files = []
nicholas@882 11 for filename in listdir(PSEUDO_PATH):
nicholas@882 12 if filename.endswith('.xml'):
nicholas@882 13 pseudo_files.append(filename)
nicholas@882 14
BrechtDeMan@881 15 curSaveIndex = 0;
BrechtDeMan@881 16 curFileName = 'test-0.xml'
BrechtDeMan@881 17 while(path.isfile('saves/'+curFileName)):
BrechtDeMan@881 18 curSaveIndex += 1;
BrechtDeMan@881 19 curFileName = 'test-'+str(curSaveIndex)+'.xml'
nicholas@872 20
nicholas@882 21 print "Next save - " + curFileName
nicholas@882 22 pseudo_index = curSaveIndex % len(pseudo_files)
nicholas@882 23 print "Next test - " + pseudo_files[pseudo_index]
nicholas@872 24
BrechtDeMan@881 25 def send404(s):
BrechtDeMan@881 26 s.send_response(404)
BrechtDeMan@881 27 s.send_header("Content-type", "text/html")
BrechtDeMan@881 28 s.end_headers()
BrechtDeMan@881 29
BrechtDeMan@881 30 def processFile(s):
BrechtDeMan@881 31 s.path = s.path[1:len(s.path)]
BrechtDeMan@881 32 st = s.path.rsplit(',')
BrechtDeMan@881 33 lenSt = len(st)
BrechtDeMan@881 34 fmt = st[lenSt-1].rsplit('.')
BrechtDeMan@881 35 size = path.getsize(urllib2.unquote(s.path))
BrechtDeMan@881 36 fileDump = open(urllib2.unquote(s.path))
BrechtDeMan@881 37 s.send_response(200)
BrechtDeMan@881 38
BrechtDeMan@881 39 if (fmt[1] == 'html'):
BrechtDeMan@881 40 s.send_header("Content-type", 'text/html')
BrechtDeMan@881 41 elif (fmt[1] == 'css'):
BrechtDeMan@881 42 s.send_header("Content-type", 'text/css')
BrechtDeMan@881 43 elif (fmt[1] == 'js'):
BrechtDeMan@881 44 s.send_header("Content-type", 'application/javascript')
BrechtDeMan@881 45 else:
BrechtDeMan@881 46 s.send_header("Content-type", 'application/octet-stream')
BrechtDeMan@881 47 s.send_header("Content-Length", size)
BrechtDeMan@881 48 s.end_headers()
BrechtDeMan@881 49 s.wfile.write(fileDump.read())
BrechtDeMan@881 50 fileDump.close()
BrechtDeMan@881 51
BrechtDeMan@881 52 def saveFile(self):
BrechtDeMan@881 53 global curFileName
BrechtDeMan@881 54 global curSaveIndex
BrechtDeMan@881 55 varLen = int(self.headers['Content-Length'])
BrechtDeMan@881 56 postVars = self.rfile.read(varLen)
BrechtDeMan@881 57 print curFileName
BrechtDeMan@881 58 file = open('saves/'+curFileName,'w')
BrechtDeMan@881 59 curSaveIndex += 1;
BrechtDeMan@881 60 curFileName = 'test-'+str(curSaveIndex)+'.xml'
BrechtDeMan@881 61 print curFileName
BrechtDeMan@881 62 file.write(postVars)
BrechtDeMan@881 63 file.close()
BrechtDeMan@881 64 self.send_response(200)
BrechtDeMan@881 65 self.send_header("Content-type", "text/xml")
BrechtDeMan@881 66 self.end_headers()
BrechtDeMan@881 67 self.wfile.write('<response><state>OK</state><file>saves/'+curFileName+'</file></response>')
nicholas@872 68
BrechtDeMan@881 69 class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
BrechtDeMan@881 70 def do_HEAD(s):
nicholas@882 71 s.send_response(200)
nicholas@882 72 s.send_header("Content-type", "text/html")
nicholas@882 73 s.end_headers()
BrechtDeMan@881 74 def do_GET(request):
nicholas@882 75 global pseudo_index
nicholas@882 76 global pseudo_files
nicholas@882 77 global PSEUDO_PATH
BrechtDeMan@881 78 if(request.client_address[0] == "127.0.0.1"):
BrechtDeMan@881 79 if (request.path == "/favicon.ico"):
BrechtDeMan@881 80 send404(request)
BrechtDeMan@881 81 else:
BrechtDeMan@881 82 if (request.path == '/'):
BrechtDeMan@881 83 request.path = '/index.html'
nicholas@882 84 elif (request.path == '/pseudo.xml'):
nicholas@882 85 request.path = '/'+PSEUDO_PATH + pseudo_files[pseudo_index]
nicholas@882 86 print request.path
nicholas@882 87 pseudo_index += 1
nicholas@882 88 pseudo_index %= len(pseudo_files)
BrechtDeMan@881 89 processFile(request)
BrechtDeMan@881 90 else:
BrechtDeMan@881 91 send404(request)
nicholas@882 92
BrechtDeMan@881 93 def do_POST(request):
BrechtDeMan@881 94 if(request.client_address[0] == "127.0.0.1"):
BrechtDeMan@881 95 if (request.path == "/save"):
BrechtDeMan@881 96 saveFile(request)
BrechtDeMan@881 97 else:
BrechtDeMan@881 98 send404(request)
BrechtDeMan@881 99
BrechtDeMan@881 100 def run(server_class=BaseHTTPServer.HTTPServer,
BrechtDeMan@881 101 handler_class=MyHandler):
BrechtDeMan@881 102 server_address = ('', 8000)
BrechtDeMan@881 103 httpd = server_class(server_address, handler_class)
BrechtDeMan@881 104 httpd.serve_forever()
BrechtDeMan@881 105
BrechtDeMan@881 106 run()