annotate pythonServer.py @ 371:ad267c5e32ae

Merge of dev into default/master
author Nicholas Jillings <n.g.r.jillings@se14.qmul.ac.uk>
date Fri, 04 Dec 2015 11:00:55 +0000
parents 77d1cb86334b
children bcab1f6d32e6 71017b71a180
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 file.write(postVars)
b@242 66 file.close()
nicholas@361 67 try:
nicholas@361 68 wbytes = os.path.getsize('saves/'+curFileName)
nicholas@361 69 except OSError:
nicholas@361 70 self.send_response(200)
nicholas@361 71 self.send_header("Content-type", "text/xml")
nicholas@361 72 self.end_headers()
nicholas@361 73 self.wfile.write('<response state="error"><message>Could not open file</message></response>')
b@242 74 self.send_response(200)
b@242 75 self.send_header("Content-type", "text/xml")
b@242 76 self.end_headers()
nicholas@361 77 self.wfile.write('<response state="OK"><message>OK</message><file bytes="'+str(wbytes)+'">"saves/'+curFileName+'"</file></response>')
nicholas@361 78 curSaveIndex += 1
nicholas@361 79 curFileName = 'test-'+str(curSaveIndex)+'.xml'
nicholas@10 80
b@242 81 class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
b@242 82 def do_HEAD(s):
nicholas@244 83 s.send_response(200)
nicholas@244 84 s.send_header("Content-type", "text/html")
nicholas@244 85 s.end_headers()
b@242 86 def do_GET(request):
nicholas@244 87 global pseudo_index
nicholas@244 88 global pseudo_files
nicholas@244 89 global PSEUDO_PATH
b@242 90 if(request.client_address[0] == "127.0.0.1"):
b@242 91 if (request.path == "/favicon.ico"):
b@242 92 send404(request)
b@242 93 else:
b@242 94 if (request.path == '/'):
b@242 95 request.path = '/index.html'
nicholas@244 96 elif (request.path == '/pseudo.xml'):
nicholas@244 97 request.path = '/'+PSEUDO_PATH + pseudo_files[pseudo_index]
nicholas@244 98 print request.path
nicholas@244 99 pseudo_index += 1
nicholas@244 100 pseudo_index %= len(pseudo_files)
b@242 101 processFile(request)
b@242 102 else:
b@242 103 send404(request)
nicholas@244 104
b@242 105 def do_POST(request):
b@242 106 if(request.client_address[0] == "127.0.0.1"):
nicholas@361 107 if (request.path == "/save" or request.path == "/save.php"):
b@242 108 saveFile(request)
b@242 109 else:
b@242 110 send404(request)
b@242 111
b@242 112 def run(server_class=BaseHTTPServer.HTTPServer,
b@242 113 handler_class=MyHandler):
b@242 114 server_address = ('', 8000)
b@242 115 httpd = server_class(server_address, handler_class)
b@242 116 httpd.serve_forever()
b@242 117
b@242 118 run()