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