annotate pythonServer.py @ 1081:267cbf724628

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