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