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