Mercurial > hg > webaudioevaluationtool
comparison pythonServer.py @ 1088:3705f68a38b7
The version I use and works, addresses issues #1622, #1616, partially #1620
author | Giulio Moro <giuliomoro@yahoo.it> |
---|---|
date | Mon, 22 Feb 2016 04:17:19 +0000 |
parents | |
children | a4ad9e55b5b8 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 1088:3705f68a38b7 |
---|---|
1 #!/usr/bin/python | |
2 | |
3 import BaseHTTPServer | |
4 from os import walk | |
5 from os import path | |
6 from os import listdir | |
7 import inspect | |
8 import os | |
9 import urllib2 | |
10 import pickle | |
11 import datetime | |
12 | |
13 # Go to right folder. | |
14 scriptdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) # script directory | |
15 os.chdir(scriptdir) # does this work? | |
16 | |
17 PSEUDO_PATH = 'example_eval/' | |
18 pseudo_files = [] | |
19 for filename in listdir(PSEUDO_PATH): | |
20 if filename.endswith('.xml'): | |
21 pseudo_files.append(filename) | |
22 | |
23 curSaveIndex = 0; | |
24 curFileName = 'test-0.xml' | |
25 while(path.isfile('saves/'+curFileName)): | |
26 curSaveIndex += 1; | |
27 curFileName = 'test-'+str(curSaveIndex)+'.xml' | |
28 | |
29 pseudo_index = curSaveIndex % len(pseudo_files) | |
30 | |
31 print 'URL: http://localhost:8000/index.html' | |
32 | |
33 def send404(s): | |
34 s.send_response(404) | |
35 s.send_header("Content-type", "text/html") | |
36 s.end_headers() | |
37 | |
38 def processFile(s): | |
39 s.path = s.path.rsplit('?') | |
40 s.path = s.path[0] | |
41 s.path = s.path[1:len(s.path)] | |
42 st = s.path.rsplit(',') | |
43 lenSt = len(st) | |
44 fmt = st[lenSt-1].rsplit('.') | |
45 size = path.getsize(urllib2.unquote(s.path)) | |
46 fileDump = open(urllib2.unquote(s.path)) | |
47 s.send_response(200) | |
48 | |
49 if (fmt[1] == 'html'): | |
50 s.send_header("Content-type", 'text/html') | |
51 elif (fmt[1] == 'css'): | |
52 s.send_header("Content-type", 'text/css') | |
53 elif (fmt[1] == 'js'): | |
54 s.send_header("Content-type", 'application/javascript') | |
55 else: | |
56 s.send_header("Content-type", 'application/octet-stream') | |
57 s.send_header("Content-Length", size) | |
58 s.end_headers() | |
59 s.wfile.write(fileDump.read()) | |
60 fileDump.close() | |
61 | |
62 def saveFile(self): | |
63 global curFileName | |
64 global curSaveIndex | |
65 varLen = int(self.headers['Content-Length']) | |
66 postVars = self.rfile.read(varLen) | |
67 print curFileName | |
68 file = open('saves/'+curFileName,'w') | |
69 file.write(postVars) | |
70 file.close() | |
71 try: | |
72 wbytes = os.path.getsize('saves/'+curFileName) | |
73 except OSError: | |
74 self.send_response(200) | |
75 self.send_header("Content-type", "text/xml") | |
76 self.end_headers() | |
77 self.wfile.write('<response state="error"><message>Could not open file</message></response>') | |
78 self.send_response(200) | |
79 self.send_header("Content-type", "text/xml") | |
80 self.end_headers() | |
81 self.wfile.write('<response state="OK"><message>OK</message><file bytes="'+str(wbytes)+'">"saves/'+curFileName+'"</file></response>') | |
82 curSaveIndex += 1 | |
83 curFileName = 'test-'+str(curSaveIndex)+'.xml' | |
84 | |
85 class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler): | |
86 def do_HEAD(s): | |
87 s.send_response(200) | |
88 s.send_header("Content-type", "text/html") | |
89 s.end_headers() | |
90 def do_GET(request): | |
91 global pseudo_index | |
92 global pseudo_files | |
93 global PSEUDO_PATH | |
94 if(request.client_address[0] == "127.0.0.1"): | |
95 if (request.path == "/favicon.ico"): | |
96 send404(request) | |
97 else: | |
98 if (request.path == '/'): | |
99 request.path = '/index.html' | |
100 elif (request.path == '/pseudo.xml'): | |
101 request.path = '/'+PSEUDO_PATH + pseudo_files[pseudo_index] | |
102 print request.path | |
103 pseudo_index += 1 | |
104 pseudo_index %= len(pseudo_files) | |
105 processFile(request) | |
106 else: | |
107 send404(request) | |
108 | |
109 def do_POST(request): | |
110 if(request.client_address[0] == "127.0.0.1"): | |
111 if (request.path == "/save" or request.path == "/save.php"): | |
112 saveFile(request) | |
113 else: | |
114 send404(request) | |
115 | |
116 def run(server_class=BaseHTTPServer.HTTPServer, | |
117 handler_class=MyHandler): | |
118 server_address = ('', 8000) | |
119 httpd = server_class(server_address, handler_class) | |
120 httpd.serve_forever() | |
121 | |
122 run() |