annotate pythonServer.py @ 749:07c996307cbd

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