annotate pythonServer.py @ 1450:bc074d4ee760

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