annotate pythonServer.py @ 1533:317faa29ab11

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