annotate pythonServer.py @ 1169:018539fa16c5

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