Mercurial > hg > webaudioevaluationtool
comparison scripts/pythonServer.py @ 2230:4e334c942d41
Fixed pythonServer.py for python v 3.x
author | Nicholas Jillings <nicholas.jillings@mail.bcu.ac.uk> |
---|---|
date | Thu, 14 Apr 2016 20:44:35 +0100 |
parents | 4d1aa94202e3 |
children | 6ffef0759d29 |
comparison
equal
deleted
inserted
replaced
2228:3358d04605db | 2230:4e334c942d41 |
---|---|
24 | 24 |
25 # Go to right folder. | 25 # Go to right folder. |
26 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 |
27 os.chdir(scriptdir) # does this work? | 27 os.chdir(scriptdir) # does this work? |
28 | 28 |
29 PSEUDO_PATH = '../example_eval/' | 29 PSEUDO_PATH = '../tests/' |
30 pseudo_files = [] | 30 pseudo_files = [] |
31 for filename in listdir(PSEUDO_PATH): | 31 for filename in listdir(PSEUDO_PATH): |
32 print(filename) | |
32 if filename.endswith('.xml'): | 33 if filename.endswith('.xml'): |
33 pseudo_files.append(filename) | 34 pseudo_files.append(filename) |
34 | 35 |
35 curSaveIndex = 0; | 36 curSaveIndex = 0; |
36 curFileName = 'test-0.xml' | 37 curFileName = 'test-0.xml' |
37 while(path.isfile('../saves/'+curFileName)): | 38 while(path.isfile('../saves/'+curFileName)): |
38 curSaveIndex += 1; | 39 curSaveIndex += 1; |
39 curFileName = 'test-'+str(curSaveIndex)+'.xml' | 40 curFileName = 'test-'+str(curSaveIndex)+'.xml' |
40 | 41 |
41 pseudo_index = curSaveIndex % len(pseudo_files) | 42 if len(pseudo_files) > 0: |
42 | 43 pseudo_index = curSaveIndex % len(pseudo_files) |
43 if sys.version_info[0] == 2: | 44 else: |
44 print 'URL: http://localhost:8000/index.html' | 45 pseudo_index = 0 |
45 elif sys.version_info[0] == 3: | 46 |
46 print('URL: http://localhost:8000/index.html') | 47 print('URL: http://localhost:8000/index.html') |
47 | 48 |
48 def send404(s): | 49 def send404(s): |
49 s.send_response(404) | 50 s.send_response(404) |
50 s.send_header("Content-type", "text/html") | 51 s.send_header("Content-type", "text/html") |
51 s.end_headers() | 52 s.end_headers() |
80 s.path = s.path[0] | 81 s.path = s.path[0] |
81 s.path = s.path[1:len(s.path)] | 82 s.path = s.path[1:len(s.path)] |
82 st = s.path.rsplit(',') | 83 st = s.path.rsplit(',') |
83 lenSt = len(st) | 84 lenSt = len(st) |
84 fmt = st[lenSt-1].rsplit('.') | 85 fmt = st[lenSt-1].rsplit('.') |
85 fpath = "../"+urllib2.unquote(s.path) | 86 fpath = "../"+urllib2.parse.unquote(s.path) |
86 s.send_response(200) | 87 s.send_response(200) |
87 if (fmt[1] == 'html'): | 88 if (fmt[1] == 'html'): |
88 s.send_header("Content-type", 'text/html') | 89 s.send_header("Content-type", 'text/html') |
89 fileDump = open(fpath, encoding='utf-8') | 90 fileDump = open(fpath, encoding='utf-8') |
90 fileBytes = bytes(fileDump.read(), "utf-8") | 91 fileBytes = bytes(fileDump.read(), "utf-8") |
111 def keygen(s): | 112 def keygen(s): |
112 reply = "" | 113 reply = "" |
113 options = s.path.rsplit('?') | 114 options = s.path.rsplit('?') |
114 options = options[1].rsplit('=') | 115 options = options[1].rsplit('=') |
115 key = options[1] | 116 key = options[1] |
116 print key | 117 print("Registered key "+key) |
117 if os.path.isfile("saves/save-"+key+".xml"): | 118 if os.path.isfile("saves/save-"+key+".xml"): |
118 reply = "<response><state>NO</state><key>"+key+"</key></response>" | 119 reply = "<response><state>NO</state><key>"+key+"</key></response>" |
119 else: | 120 else: |
120 reply = "<response><state>OK</state><key>"+key+"</key></response>" | 121 reply = "<response><state>OK</state><key>"+key+"</key></response>" |
121 s.send_response(200) | 122 s.send_response(200) |
130 global curFileName | 131 global curFileName |
131 global curSaveIndex | 132 global curSaveIndex |
132 options = self.path.rsplit('?') | 133 options = self.path.rsplit('?') |
133 options = options[1].rsplit('=') | 134 options = options[1].rsplit('=') |
134 key = options[1] | 135 key = options[1] |
135 print key | |
136 varLen = int(self.headers['Content-Length']) | 136 varLen = int(self.headers['Content-Length']) |
137 postVars = self.rfile.read(varLen) | 137 postVars = self.rfile.read(varLen) |
138 if sys.version_info[0] == 2: | 138 print("Saving file key "+key) |
139 print "Saving file key "+key | |
140 elif sys.version_info[0] == 3: | |
141 print("Saving file key "+key) | |
142 file = open('../saves/save-'+key+'.xml','w') | 139 file = open('../saves/save-'+key+'.xml','w') |
143 file.write(postVars) | 140 file.write(postVars) |
144 file.close() | 141 file.close() |
145 try: | 142 try: |
146 wbytes = os.path.getsize('../saves/save-'+key+'.xml') | 143 wbytes = os.path.getsize('../saves/save-'+key+'.xml') |
154 self.end_headers() | 151 self.end_headers() |
155 self.wfile.write('<response state="OK"><message>OK</message><file bytes="'+str(wbytes)+'">"saves/'+curFileName+'"</file></response>') | 152 self.wfile.write('<response state="OK"><message>OK</message><file bytes="'+str(wbytes)+'">"saves/'+curFileName+'"</file></response>') |
156 curSaveIndex += 1 | 153 curSaveIndex += 1 |
157 curFileName = 'test-'+str(curSaveIndex)+'.xml' | 154 curFileName = 'test-'+str(curSaveIndex)+'.xml' |
158 | 155 |
159 class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler): | 156 def http_do_HEAD(s): |
160 def do_HEAD(s): | 157 s.send_response(200) |
161 s.send_response(200) | 158 s.send_header("Content-type", "text/html") |
162 s.send_header("Content-type", "text/html") | 159 s.end_headers() |
163 s.end_headers() | 160 |
164 def do_GET(request): | 161 def http_do_GET(request): |
165 global pseudo_index | 162 if(request.client_address[0] == "127.0.0.1"): |
166 global pseudo_files | 163 if (request.path == "/favicon.ico"): |
167 global PSEUDO_PATH | 164 send404(request) |
168 if(request.client_address[0] == "127.0.0.1"): | 165 elif (request.path.split('?',1)[0] == "/keygen.php"): |
169 if (request.path == "/favicon.ico"): | 166 keygen(request); |
170 send404(request) | 167 else: |
171 elif (request.path.split('?',1)[0] == "/keygen.php"): | 168 request.path = request.path.split('?',1)[0] |
172 keygen(request); | 169 if (request.path == '/'): |
173 else: | 170 request.path = '/index.html' |
174 if (request.path == '/'): | 171 elif (request.path == '/pseudo.xml'): |
175 request.path = '/index.html' | 172 request.path = '/'+PSEUDO_PATH + pseudo_files[pseudo_index] |
176 elif (request.path == '/pseudo.xml'): | 173 print(request.path) |
177 request.path = '/'+PSEUDO_PATH + pseudo_files[pseudo_index] | 174 pseudo_index += 1 |
178 print request.path | 175 pseudo_index %= len(pseudo_files) |
179 pseudo_index += 1 | 176 processFile(request) |
180 pseudo_index %= len(pseudo_files) | 177 else: |
181 processFile(request) | 178 send404(request) |
182 else: | 179 |
183 send404(request) | 180 def http_do_POST(request): |
184 | 181 if(request.client_address[0] == "127.0.0.1"): |
185 def do_POST(request): | 182 if (request.path.rsplit('?',1)[0] == "/save" or request.path.rsplit('?',1)[0] == "/save.php"): |
186 if(request.client_address[0] == "127.0.0.1"): | 183 saveFile(request) |
187 if (request.path.rsplit('?',1)[0] == "/save" or request.path.rsplit('?',1)[0] == "/save.php"): | 184 else: |
188 saveFile(request) | 185 send404(request) |
189 else: | 186 |
190 send404(request) | |
191 if sys.version_info[0] == 2: | 187 if sys.version_info[0] == 2: |
188 class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler): | |
189 def do_HEAD(s): | |
190 http_do_HEAD(s) | |
191 def do_GET(request): | |
192 http_do_GET(request) | |
193 def do_POST(request): | |
194 http_do_POST(request) | |
192 def run(server_class=BaseHTTPServer.HTTPServer,handler_class=MyHandler): | 195 def run(server_class=BaseHTTPServer.HTTPServer,handler_class=MyHandler): |
193 server_address = ('', 8000) | 196 server_address = ('', 8000) |
194 httpd = server_class(server_address, handler_class) | 197 httpd = server_class(server_address, handler_class) |
195 httpd.serve_forever() | 198 httpd.serve_forever() |
199 run() | |
196 elif sys.version_info[0] == 3: | 200 elif sys.version_info[0] == 3: |
201 class MyHandler(BaseHTTPRequestHandler): | |
202 def do_HEAD(s): | |
203 send404(s) | |
204 def do_GET(request): | |
205 http_do_GET(request) | |
206 def do_POST(request): | |
207 http_do_POST(request) | |
197 def run(server_class=HTTPServer,handler_class=MyHandler): | 208 def run(server_class=HTTPServer,handler_class=MyHandler): |
198 server_address = ('', 8000) | 209 server_address = ('', 8000) |
199 httpd = server_class(server_address, handler_class) | 210 httpd = server_class(server_address, handler_class) |
200 httpd.serve_forever() | 211 httpd.serve_forever() |
201 | 212 run() |
202 run() |