annotate pythonServer-3.py @ 2202:61c8e13f1e2e

Merge branch 'master' of https://github.com/BrechtDeMan/WebAudioEvaluationTool
author www-data <www-data@sucuk.dcs.qmul.ac.uk>
date Fri, 08 Apr 2016 13:20:54 +0100
parents c07b9e2312ba
children
rev   line source
n@1090 1 from http.server import BaseHTTPRequestHandler, HTTPServer
n@1090 2 from os import walk
n@1090 3 from os import path
n@1090 4 from os import listdir
n@1090 5 import inspect
n@1090 6 import os
n@1090 7 import urllib as urllib2
n@1090 8 import pickle
n@1090 9 import datetime
n@1090 10
n@1090 11 # Go to right folder.
n@1090 12 scriptdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) # script directory
n@1090 13 os.chdir(scriptdir) # does this work?
n@1090 14
n@1090 15 PSEUDO_PATH = 'example_eval/'
n@1090 16 pseudo_files = []
n@1090 17 for filename in listdir(PSEUDO_PATH):
n@1090 18 if filename.endswith('.xml'):
n@1090 19 pseudo_files.append(filename)
n@1090 20
n@1090 21 curSaveIndex = 0;
n@1090 22 curFileName = 'test-0.xml'
n@1090 23 while(path.isfile('saves/'+curFileName)):
n@1090 24 curSaveIndex += 1;
n@1090 25 curFileName = 'test-'+str(curSaveIndex)+'.xml'
n@1090 26
n@1090 27 pseudo_index = curSaveIndex % len(pseudo_files)
n@1090 28
n@1090 29 print('URL: http://localhost:8000/index.html')
n@1090 30
n@1090 31 def send404(s):
n@1090 32 s.send_response(404)
n@1090 33 s.send_header("Content-type", "text/html")
n@1090 34 s.end_headers()
n@1090 35
n@1090 36 def processFile(s):
n@1090 37 s.path = s.path.rsplit('?')
n@1090 38 s.path = s.path[0]
n@1090 39 s.path = s.path[1:len(s.path)]
n@1090 40 st = s.path.rsplit(',')
n@1090 41 lenSt = len(st)
n@1090 42 fmt = st[lenSt-1].rsplit('.')
n@1090 43 s.send_response(200)
n@1090 44 if (fmt[1] == 'html'):
n@1090 45 s.send_header("Content-type", 'text/html')
n@1090 46 fileDump = open(urllib2.parse.unquote(s.path), encoding='utf-8')
n@1090 47 fileBytes = bytes(fileDump.read(), "utf-8")
n@1090 48 fileDump.close()
n@1090 49 elif (fmt[1] == 'css'):
n@1090 50 s.send_header("Content-type", 'text/css')
n@1090 51 fileDump = open(urllib2.parse.unquote(s.path), encoding='utf-8')
n@1090 52 fileBytes = bytes(fileDump.read(), "utf-8")
n@1090 53 fileDump.close()
n@1090 54 elif (fmt[1] == 'js'):
n@1090 55 s.send_header("Content-type", 'application/javascript')
n@1090 56 fileDump = open(urllib2.parse.unquote(s.path), encoding='utf-8')
n@1090 57 fileBytes = bytes(fileDump.read(), "utf-8")
n@1090 58 fileDump.close()
n@1090 59 else:
n@1090 60 s.send_header("Content-type", 'application/octet-stream')
n@1090 61 fileDump = open(urllib2.parse.unquote(s.path), 'rb')
n@1090 62 fileBytes = fileDump.read()
n@1090 63 fileDump.close()
n@1090 64 s.send_header("Content-Length", len(fileBytes))
n@1090 65 s.end_headers()
n@1090 66 s.wfile.write(fileBytes)
n@1090 67
n@1090 68 def saveFile(self):
n@1090 69 global curFileName
n@1090 70 global curSaveIndex
n@1090 71 varLen = int(self.headers['Content-Length'])
n@1090 72 postVars = self.rfile.read(varLen)
n@1090 73 print(curFileName)
n@1090 74 file = open('saves/'+curFileName,'w')
n@1090 75 file.write(postVars.decode("utf-8"))
n@1090 76 file.close()
n@1090 77 try:
n@1090 78 wbytes = os.path.getsize('saves/'+curFileName)
n@1090 79 except OSError:
n@1090 80 self.send_response(200)
n@1090 81 self.send_header("Content-type", "text/xml")
n@1090 82 self.end_headers()
n@1090 83 self.wfile.write('<response state="error"><message>Could not open file</message></response>')
n@1090 84 self.send_response(200)
n@1090 85 self.send_header("Content-type", "text/xml")
n@1090 86 self.end_headers()
n@1090 87 self.wfile.write(bytes('<response state="OK"><message>OK</message><file bytes="'+str(wbytes)+'">"saves/'+curFileName+'"</file></response>','utf-8'))
n@1090 88 curSaveIndex += 1
n@1090 89 curFileName = 'test-'+str(curSaveIndex)+'.xml'
n@1090 90
n@1090 91 class MyHandler(BaseHTTPRequestHandler):
n@1090 92 def do_HEAD(s):
n@1090 93 s.send_response(200)
n@1090 94 s.send_header("Content-type", "text/html")
n@1090 95 s.end_headers()
n@1090 96 def do_GET(request):
n@1090 97 global pseudo_index
n@1090 98 global pseudo_files
n@1090 99 global PSEUDO_PATH
n@1090 100 if(request.client_address[0] == "127.0.0.1"):
n@1090 101 if (request.path == "/favicon.ico"):
n@1090 102 send404(request)
n@1090 103 else:
n@1090 104 if (request.path == '/'):
n@1090 105 request.path = '/index.html'
n@1090 106 elif (request.path == '/pseudo.xml'):
n@1090 107 request.path = '/'+PSEUDO_PATH + pseudo_files[pseudo_index]
n@1090 108 print(request.path)
n@1090 109 pseudo_index += 1
n@1090 110 pseudo_index %= len(pseudo_files)
n@1090 111 processFile(request)
n@1090 112 else:
n@1090 113 send404(request)
n@1090 114
n@1090 115 def do_POST(request):
n@1090 116 if(request.client_address[0] == "127.0.0.1"):
n@1090 117 if (request.path == "/save" or request.path == "/save.php"):
n@1090 118 saveFile(request)
n@1090 119 else:
n@1090 120 send404(request)
n@1090 121
n@1090 122 def run(server_class=HTTPServer,
n@1090 123 handler_class=MyHandler):
n@1090 124 server_address = ('', 8000)
n@1090 125 httpd = server_class(server_address, handler_class)
n@1090 126 httpd.serve_forever()
n@1090 127
n@1090 128 run()