Mercurial > hg > webaudioevaluationtool
comparison scripts/pythonServer.py @ 2222:4d1aa94202e3
Merged pythonServer-3 into unified pythonServer. Removed pythonServer-legacy. Updated pythonServer to work in scripts subdir.
author | Nicholas Jillings <nicholas.jillings@mail.bcu.ac.uk> |
---|---|
date | Thu, 14 Apr 2016 13:19:17 +0100 |
parents | 4615745ed30c |
children | 4e334c942d41 |
comparison
equal
deleted
inserted
replaced
2221:6dbee0a43792 | 2222:4d1aa94202e3 |
---|---|
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 | 2 |
3 import BaseHTTPServer | 3 # Detect the Python version to switch code between 2.x and 3.x |
4 # http://stackoverflow.com/questions/9079036/detect-python-version-at-runtime | |
5 import sys | |
6 | |
4 from os import walk | 7 from os import walk |
5 from os import path | 8 from os import path |
6 from os import listdir | 9 from os import listdir |
7 import inspect | 10 import inspect |
8 import os | 11 import os |
9 import urllib2 | |
10 import urlparse | |
11 import pickle | 12 import pickle |
12 import datetime | 13 import datetime |
14 | |
15 if sys.version_info[0] == 2: | |
16 # Version 2.x | |
17 import BaseHTTPServer | |
18 import urllib2 | |
19 import urlparse | |
20 elif sys.version_info[0] == 3: | |
21 # Version 3.x | |
22 from http.server import BaseHTTPRequestHandler, HTTPServer | |
23 import urllib as urllib2 | |
13 | 24 |
14 # Go to right folder. | 25 # Go to right folder. |
15 scriptdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) # script directory | 26 scriptdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) # script directory |
16 os.chdir(scriptdir) # does this work? | 27 os.chdir(scriptdir) # does this work? |
17 | 28 |
18 PSEUDO_PATH = 'example_eval/' | 29 PSEUDO_PATH = '../example_eval/' |
19 pseudo_files = [] | 30 pseudo_files = [] |
20 for filename in listdir(PSEUDO_PATH): | 31 for filename in listdir(PSEUDO_PATH): |
21 if filename.endswith('.xml'): | 32 if filename.endswith('.xml'): |
22 pseudo_files.append(filename) | 33 pseudo_files.append(filename) |
23 | 34 |
24 curSaveIndex = 0; | 35 curSaveIndex = 0; |
25 curFileName = 'test-0.xml' | 36 curFileName = 'test-0.xml' |
26 while(path.isfile('saves/'+curFileName)): | 37 while(path.isfile('../saves/'+curFileName)): |
27 curSaveIndex += 1; | 38 curSaveIndex += 1; |
28 curFileName = 'test-'+str(curSaveIndex)+'.xml' | 39 curFileName = 'test-'+str(curSaveIndex)+'.xml' |
29 | 40 |
30 pseudo_index = curSaveIndex % len(pseudo_files) | 41 pseudo_index = curSaveIndex % len(pseudo_files) |
31 | 42 |
32 print 'URL: http://localhost:8000/index.html' | 43 if sys.version_info[0] == 2: |
44 print 'URL: http://localhost:8000/index.html' | |
45 elif sys.version_info[0] == 3: | |
46 print('URL: http://localhost:8000/index.html') | |
33 | 47 |
34 def send404(s): | 48 def send404(s): |
35 s.send_response(404) | 49 s.send_response(404) |
36 s.send_header("Content-type", "text/html") | 50 s.send_header("Content-type", "text/html") |
37 s.end_headers() | 51 s.end_headers() |
38 | 52 |
39 def processFile(s): | 53 def processFile(s): |
40 s.path = s.path.rsplit('?') | 54 if sys.version_info[0] == 2: |
41 s.path = s.path[0] | 55 s.path = s.path.rsplit('?') |
42 s.path = s.path[1:len(s.path)] | 56 s.path = s.path[0] |
43 st = s.path.rsplit(',') | 57 s.path = s.path[1:len(s.path)] |
44 lenSt = len(st) | 58 st = s.path.rsplit(',') |
45 fmt = st[lenSt-1].rsplit('.') | 59 lenSt = len(st) |
46 size = path.getsize(urllib2.unquote(s.path)) | 60 fmt = st[lenSt-1].rsplit('.') |
47 fileDump = open(urllib2.unquote(s.path)) | 61 fpath = "../"+urllib2.unquote(s.path) |
48 s.send_response(200) | 62 size = path.getsize(fpath) |
49 | 63 fileDump = open(fpath) |
50 if (fmt[1] == 'html'): | 64 s.send_response(200) |
51 s.send_header("Content-type", 'text/html') | 65 |
52 elif (fmt[1] == 'css'): | 66 if (fmt[1] == 'html'): |
53 s.send_header("Content-type", 'text/css') | 67 s.send_header("Content-type", 'text/html') |
54 elif (fmt[1] == 'js'): | 68 elif (fmt[1] == 'css'): |
55 s.send_header("Content-type", 'application/javascript') | 69 s.send_header("Content-type", 'text/css') |
56 else: | 70 elif (fmt[1] == 'js'): |
57 s.send_header("Content-type", 'application/octet-stream') | 71 s.send_header("Content-type", 'application/javascript') |
58 s.send_header("Content-Length", size) | 72 else: |
59 s.end_headers() | 73 s.send_header("Content-type", 'application/octet-stream') |
60 s.wfile.write(fileDump.read()) | 74 s.send_header("Content-Length", size) |
61 fileDump.close() | 75 s.end_headers() |
76 s.wfile.write(fileDump.read()) | |
77 fileDump.close() | |
78 elif sys.version_info[0] == 3: | |
79 s.path = s.path.rsplit('?') | |
80 s.path = s.path[0] | |
81 s.path = s.path[1:len(s.path)] | |
82 st = s.path.rsplit(',') | |
83 lenSt = len(st) | |
84 fmt = st[lenSt-1].rsplit('.') | |
85 fpath = "../"+urllib2.unquote(s.path) | |
86 s.send_response(200) | |
87 if (fmt[1] == 'html'): | |
88 s.send_header("Content-type", 'text/html') | |
89 fileDump = open(fpath, encoding='utf-8') | |
90 fileBytes = bytes(fileDump.read(), "utf-8") | |
91 fileDump.close() | |
92 elif (fmt[1] == 'css'): | |
93 s.send_header("Content-type", 'text/css') | |
94 fileDump = open(fpath, encoding='utf-8') | |
95 fileBytes = bytes(fileDump.read(), "utf-8") | |
96 fileDump.close() | |
97 elif (fmt[1] == 'js'): | |
98 s.send_header("Content-type", 'application/javascript') | |
99 fileDump = open(fpath, encoding='utf-8') | |
100 fileBytes = bytes(fileDump.read(), "utf-8") | |
101 fileDump.close() | |
102 else: | |
103 s.send_header("Content-type", 'application/octet-stream') | |
104 fileDump = open(fpath, 'rb') | |
105 fileBytes = fileDump.read() | |
106 fileDump.close() | |
107 s.send_header("Content-Length", len(fileBytes)) | |
108 s.end_headers() | |
109 s.wfile.write(fileBytes) | |
62 | 110 |
63 def keygen(s): | 111 def keygen(s): |
64 reply = "" | 112 reply = "" |
65 options = s.path.rsplit('?') | 113 options = s.path.rsplit('?') |
66 options = options[1].rsplit('=') | 114 options = options[1].rsplit('=') |
72 reply = "<response><state>OK</state><key>"+key+"</key></response>" | 120 reply = "<response><state>OK</state><key>"+key+"</key></response>" |
73 s.send_response(200) | 121 s.send_response(200) |
74 s.send_header("Content-type", "application/xml") | 122 s.send_header("Content-type", "application/xml") |
75 s.end_headers() | 123 s.end_headers() |
76 s.wfile.write(reply) | 124 s.wfile.write(reply) |
77 file = open("saves/save-"+key+".xml",'w') | 125 file = open("../saves/save-"+key+".xml",'w') |
78 file.write("<waetresult key="+key+"/>") | 126 file.write("<waetresult key="+key+"/>") |
79 file.close(); | 127 file.close(); |
80 | 128 |
81 def saveFile(self): | 129 def saveFile(self): |
82 global curFileName | 130 global curFileName |
83 global curSaveIndex | 131 global curSaveIndex |
84 options = self.path.rsplit('?') | 132 options = self.path.rsplit('?') |
85 options = options[1].rsplit('=') | 133 options = options[1].rsplit('=') |
86 key = options[1] | 134 key = options[1] |
87 print key | 135 print key |
88 varLen = int(self.headers['Content-Length']) | 136 varLen = int(self.headers['Content-Length']) |
89 postVars = self.rfile.read(varLen) | 137 postVars = self.rfile.read(varLen) |
90 print "Saving file key "+key | 138 if sys.version_info[0] == 2: |
91 file = open('saves/save-'+key+'.xml','w') | 139 print "Saving file key "+key |
92 file.write(postVars) | 140 elif sys.version_info[0] == 3: |
93 file.close() | 141 print("Saving file key "+key) |
94 try: | 142 file = open('../saves/save-'+key+'.xml','w') |
95 wbytes = os.path.getsize('saves/save-'+key+'.xml') | 143 file.write(postVars) |
96 except OSError: | 144 file.close() |
97 self.send_response(200) | 145 try: |
98 self.send_header("Content-type", "text/xml") | 146 wbytes = os.path.getsize('../saves/save-'+key+'.xml') |
99 self.end_headers() | 147 except OSError: |
100 self.wfile.write('<response state="error"><message>Could not open file</message></response>') | 148 self.send_response(200) |
101 self.send_response(200) | 149 self.send_header("Content-type", "text/xml") |
102 self.send_header("Content-type", "text/xml") | 150 self.end_headers() |
103 self.end_headers() | 151 self.wfile.write('<response state="error"><message>Could not open file</message></response>') |
104 self.wfile.write('<response state="OK"><message>OK</message><file bytes="'+str(wbytes)+'">"saves/'+curFileName+'"</file></response>') | 152 self.send_response(200) |
105 curSaveIndex += 1 | 153 self.send_header("Content-type", "text/xml") |
106 curFileName = 'test-'+str(curSaveIndex)+'.xml' | 154 self.end_headers() |
155 self.wfile.write('<response state="OK"><message>OK</message><file bytes="'+str(wbytes)+'">"saves/'+curFileName+'"</file></response>') | |
156 curSaveIndex += 1 | |
157 curFileName = 'test-'+str(curSaveIndex)+'.xml' | |
107 | 158 |
108 class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler): | 159 class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler): |
109 def do_HEAD(s): | 160 def do_HEAD(s): |
110 s.send_response(200) | 161 s.send_response(200) |
111 s.send_header("Content-type", "text/html") | 162 s.send_header("Content-type", "text/html") |
135 if(request.client_address[0] == "127.0.0.1"): | 186 if(request.client_address[0] == "127.0.0.1"): |
136 if (request.path.rsplit('?',1)[0] == "/save" or request.path.rsplit('?',1)[0] == "/save.php"): | 187 if (request.path.rsplit('?',1)[0] == "/save" or request.path.rsplit('?',1)[0] == "/save.php"): |
137 saveFile(request) | 188 saveFile(request) |
138 else: | 189 else: |
139 send404(request) | 190 send404(request) |
140 | 191 if sys.version_info[0] == 2: |
141 def run(server_class=BaseHTTPServer.HTTPServer, | 192 def run(server_class=BaseHTTPServer.HTTPServer,handler_class=MyHandler): |
142 handler_class=MyHandler): | 193 server_address = ('', 8000) |
143 server_address = ('', 8000) | 194 httpd = server_class(server_address, handler_class) |
144 httpd = server_class(server_address, handler_class) | 195 httpd.serve_forever() |
145 httpd.serve_forever() | 196 elif sys.version_info[0] == 3: |
197 def run(server_class=HTTPServer,handler_class=MyHandler): | |
198 server_address = ('', 8000) | |
199 httpd = server_class(server_address, handler_class) | |
200 httpd.serve_forever() | |
146 | 201 |
147 run() | 202 run() |