annotate pythonServer.py @ 1491:5fd7e8f4313f

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