annotate pythonServer.py @ 1410:c0098cee84f2

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