djmoffat@718: import BaseHTTPServer djmoffat@718: from os import walk djmoffat@718: from os import path djmoffat@718: from os import listdir djmoffat@718: import inspect djmoffat@718: import os djmoffat@718: import urllib2 djmoffat@718: import pickle djmoffat@718: import datetime djmoffat@718: djmoffat@718: # Go to right folder. djmoffat@718: scriptdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) # script directory djmoffat@718: os.chdir(scriptdir) # does this work? djmoffat@718: djmoffat@718: PSEUDO_PATH = 'example_eval/' djmoffat@718: pseudo_files = [] djmoffat@718: for filename in listdir(PSEUDO_PATH): djmoffat@718: if filename.endswith('.xml'): djmoffat@718: pseudo_files.append(filename) djmoffat@718: djmoffat@718: curSaveIndex = 0; djmoffat@718: curFileName = 'test-0.xml' djmoffat@718: while(path.isfile('saves/'+curFileName)): djmoffat@718: curSaveIndex += 1; djmoffat@718: curFileName = 'test-'+str(curSaveIndex)+'.xml' djmoffat@718: djmoffat@718: print "Next save - " + curFileName djmoffat@718: pseudo_index = curSaveIndex % len(pseudo_files) djmoffat@718: print "Next test in pseudo-random queue - " + pseudo_files[pseudo_index] djmoffat@718: djmoffat@718: def send404(s): djmoffat@718: s.send_response(404) djmoffat@718: s.send_header("Content-type", "text/html") djmoffat@718: s.end_headers() djmoffat@718: djmoffat@718: def processFile(s): djmoffat@718: s.path = s.path[1:len(s.path)] djmoffat@718: st = s.path.rsplit(',') djmoffat@718: lenSt = len(st) djmoffat@718: fmt = st[lenSt-1].rsplit('.') djmoffat@718: size = path.getsize(urllib2.unquote(s.path)) djmoffat@718: fileDump = open(urllib2.unquote(s.path)) djmoffat@718: s.send_response(200) djmoffat@718: djmoffat@718: if (fmt[1] == 'html'): djmoffat@718: s.send_header("Content-type", 'text/html') djmoffat@718: elif (fmt[1] == 'css'): djmoffat@718: s.send_header("Content-type", 'text/css') djmoffat@718: elif (fmt[1] == 'js'): djmoffat@718: s.send_header("Content-type", 'application/javascript') djmoffat@718: else: djmoffat@718: s.send_header("Content-type", 'application/octet-stream') djmoffat@718: s.send_header("Content-Length", size) djmoffat@718: s.end_headers() djmoffat@718: s.wfile.write(fileDump.read()) djmoffat@718: fileDump.close() djmoffat@718: djmoffat@718: def saveFile(self): djmoffat@718: global curFileName djmoffat@718: global curSaveIndex djmoffat@718: varLen = int(self.headers['Content-Length']) djmoffat@718: postVars = self.rfile.read(varLen) djmoffat@718: print curFileName djmoffat@718: file = open('saves/'+curFileName,'w') djmoffat@718: curSaveIndex += 1; djmoffat@718: curFileName = 'test-'+str(curSaveIndex)+'.xml' djmoffat@718: print curFileName djmoffat@718: file.write(postVars) djmoffat@718: file.close() djmoffat@718: self.send_response(200) djmoffat@718: self.send_header("Content-type", "text/xml") djmoffat@718: self.end_headers() djmoffat@718: self.wfile.write('OKsaves/'+curFileName+'') djmoffat@718: djmoffat@718: class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler): djmoffat@718: def do_HEAD(s): djmoffat@718: s.send_response(200) djmoffat@718: s.send_header("Content-type", "text/html") djmoffat@718: s.end_headers() djmoffat@718: def do_GET(request): djmoffat@718: global pseudo_index djmoffat@718: global pseudo_files djmoffat@718: global PSEUDO_PATH djmoffat@718: if(request.client_address[0] == "127.0.0.1"): djmoffat@718: if (request.path == "/favicon.ico"): djmoffat@718: send404(request) djmoffat@718: else: djmoffat@718: if (request.path == '/'): djmoffat@718: request.path = '/index.html' djmoffat@718: elif (request.path == '/pseudo.xml'): djmoffat@718: request.path = '/'+PSEUDO_PATH + pseudo_files[pseudo_index] djmoffat@718: print request.path djmoffat@718: pseudo_index += 1 djmoffat@718: pseudo_index %= len(pseudo_files) djmoffat@718: processFile(request) djmoffat@718: else: djmoffat@718: send404(request) djmoffat@718: djmoffat@718: def do_POST(request): djmoffat@718: if(request.client_address[0] == "127.0.0.1"): djmoffat@718: if (request.path == "/save"): djmoffat@718: saveFile(request) djmoffat@718: else: djmoffat@718: send404(request) djmoffat@718: djmoffat@718: def run(server_class=BaseHTTPServer.HTTPServer, djmoffat@718: handler_class=MyHandler): djmoffat@718: server_address = ('', 8000) djmoffat@718: httpd = server_class(server_address, handler_class) djmoffat@718: httpd.serve_forever() djmoffat@718: djmoffat@718: run()