annotate pythonServer.py @ 1079:24eeac8994bf

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