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