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