annotate pythonServer-3.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
rev   line source
n@1116 1 from http.server import BaseHTTPRequestHandler, HTTPServer
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 urllib as 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 s.send_response(200)
n@1116 44 if (fmt[1] == 'html'):
n@1116 45 s.send_header("Content-type", 'text/html')
n@1116 46 fileDump = open(urllib2.parse.unquote(s.path), encoding='utf-8')
n@1116 47 fileBytes = bytes(fileDump.read(), "utf-8")
n@1116 48 fileDump.close()
n@1116 49 elif (fmt[1] == 'css'):
n@1116 50 s.send_header("Content-type", 'text/css')
n@1116 51 fileDump = open(urllib2.parse.unquote(s.path), encoding='utf-8')
n@1116 52 fileBytes = bytes(fileDump.read(), "utf-8")
n@1116 53 fileDump.close()
n@1116 54 elif (fmt[1] == 'js'):
n@1116 55 s.send_header("Content-type", 'application/javascript')
n@1116 56 fileDump = open(urllib2.parse.unquote(s.path), encoding='utf-8')
n@1116 57 fileBytes = bytes(fileDump.read(), "utf-8")
n@1116 58 fileDump.close()
n@1116 59 else:
n@1116 60 s.send_header("Content-type", 'application/octet-stream')
n@1116 61 fileDump = open(urllib2.parse.unquote(s.path), 'rb')
n@1116 62 fileBytes = fileDump.read()
n@1116 63 fileDump.close()
n@1116 64 s.send_header("Content-Length", len(fileBytes))
n@1116 65 s.end_headers()
n@1116 66 s.wfile.write(fileBytes)
n@1116 67
n@1116 68 def saveFile(self):
n@1116 69 global curFileName
n@1116 70 global curSaveIndex
n@1116 71 varLen = int(self.headers['Content-Length'])
n@1116 72 postVars = self.rfile.read(varLen)
n@1116 73 print(curFileName)
n@1116 74 file = open('saves/'+curFileName,'w')
n@1116 75 file.write(postVars.decode("utf-8"))
n@1116 76 file.close()
n@1116 77 try:
n@1116 78 wbytes = os.path.getsize('saves/'+curFileName)
n@1116 79 except OSError:
n@1116 80 self.send_response(200)
n@1116 81 self.send_header("Content-type", "text/xml")
n@1116 82 self.end_headers()
n@1116 83 self.wfile.write('<response state="error"><message>Could not open file</message></response>')
n@1116 84 self.send_response(200)
n@1116 85 self.send_header("Content-type", "text/xml")
n@1116 86 self.end_headers()
n@1116 87 self.wfile.write(bytes('<response state="OK"><message>OK</message><file bytes="'+str(wbytes)+'">"saves/'+curFileName+'"</file></response>','utf-8'))
n@1116 88 curSaveIndex += 1
n@1116 89 curFileName = 'test-'+str(curSaveIndex)+'.xml'
n@1116 90
n@1116 91 class MyHandler(BaseHTTPRequestHandler):
n@1116 92 def do_HEAD(s):
n@1116 93 s.send_response(200)
n@1116 94 s.send_header("Content-type", "text/html")
n@1116 95 s.end_headers()
n@1116 96 def do_GET(request):
n@1116 97 global pseudo_index
n@1116 98 global pseudo_files
n@1116 99 global PSEUDO_PATH
n@1116 100 if(request.client_address[0] == "127.0.0.1"):
n@1116 101 if (request.path == "/favicon.ico"):
n@1116 102 send404(request)
n@1116 103 else:
n@1116 104 if (request.path == '/'):
n@1116 105 request.path = '/index.html'
n@1116 106 elif (request.path == '/pseudo.xml'):
n@1116 107 request.path = '/'+PSEUDO_PATH + pseudo_files[pseudo_index]
n@1116 108 print(request.path)
n@1116 109 pseudo_index += 1
n@1116 110 pseudo_index %= len(pseudo_files)
n@1116 111 processFile(request)
n@1116 112 else:
n@1116 113 send404(request)
n@1116 114
n@1116 115 def do_POST(request):
n@1116 116 if(request.client_address[0] == "127.0.0.1"):
n@1116 117 if (request.path == "/save" or request.path == "/save.php"):
n@1116 118 saveFile(request)
n@1116 119 else:
n@1116 120 send404(request)
n@1116 121
n@1116 122 def run(server_class=HTTPServer,
n@1116 123 handler_class=MyHandler):
n@1116 124 server_address = ('', 8000)
n@1116 125 httpd = server_class(server_address, handler_class)
n@1116 126 httpd.serve_forever()
n@1116 127
n@1116 128 run()