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