annotate pythonServer.py @ 2085:11328fe5d16d

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