annotate pythonServer.py @ 1386:345974f8794a

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