annotate Perceptual Evaluation/webaudioevaluationtool/pythonServer.py @ 0:55c282f01a30 tip

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