annotate pythonServer.py @ 813:f452455b5977

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