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