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