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