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