annotate pythonServer.py @ 1118:3edcbbea168b

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