b@2211: #!/usr/bin/python b@2211: nicholas@2222: # Detect the Python version to switch code between 2.x and 3.x nicholas@2222: # http://stackoverflow.com/questions/9079036/detect-python-version-at-runtime nicholas@2222: import sys nicholas@2222: b@2211: from os import walk b@2211: from os import path b@2211: from os import listdir b@2211: import inspect b@2211: import os b@2211: import pickle b@2211: import datetime b@2211: nicholas@2222: if sys.version_info[0] == 2: nicholas@2222: # Version 2.x nicholas@2222: import BaseHTTPServer nicholas@2222: import urllib2 nicholas@2222: import urlparse nicholas@2222: elif sys.version_info[0] == 3: nicholas@2222: # Version 3.x nicholas@2222: from http.server import BaseHTTPRequestHandler, HTTPServer nicholas@2222: import urllib as urllib2 nicholas@2222: b@2211: # Go to right folder. b@2211: scriptdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) # script directory b@2211: os.chdir(scriptdir) # does this work? b@2211: nicholas@2230: PSEUDO_PATH = '../tests/' b@2211: pseudo_files = [] b@2211: for filename in listdir(PSEUDO_PATH): nicholas@2230: print(filename) b@2211: if filename.endswith('.xml'): b@2211: pseudo_files.append(filename) b@2211: b@2211: curSaveIndex = 0; b@2211: curFileName = 'test-0.xml' nicholas@2222: while(path.isfile('../saves/'+curFileName)): nicholas@2222: curSaveIndex += 1; nicholas@2222: curFileName = 'test-'+str(curSaveIndex)+'.xml' b@2211: nicholas@2230: if len(pseudo_files) > 0: nicholas@2230: pseudo_index = curSaveIndex % len(pseudo_files) nicholas@2230: else: nicholas@2230: pseudo_index = 0 b@2211: nicholas@2230: print('URL: http://localhost:8000/index.html') b@2211: b@2211: def send404(s): nicholas@2222: s.send_response(404) nicholas@2222: s.send_header("Content-type", "text/html") nicholas@2222: s.end_headers() b@2211: b@2211: def processFile(s): nicholas@2222: if sys.version_info[0] == 2: nicholas@2222: s.path = s.path.rsplit('?') nicholas@2222: s.path = s.path[0] nicholas@2222: s.path = s.path[1:len(s.path)] nicholas@2222: st = s.path.rsplit(',') nicholas@2222: lenSt = len(st) nicholas@2222: fmt = st[lenSt-1].rsplit('.') nicholas@2222: fpath = "../"+urllib2.unquote(s.path) nicholas@2222: size = path.getsize(fpath) nicholas@2222: fileDump = open(fpath) nicholas@2222: s.send_response(200) nicholas@2222: nicholas@2222: if (fmt[1] == 'html'): nicholas@2222: s.send_header("Content-type", 'text/html') nicholas@2222: elif (fmt[1] == 'css'): nicholas@2222: s.send_header("Content-type", 'text/css') nicholas@2222: elif (fmt[1] == 'js'): nicholas@2222: s.send_header("Content-type", 'application/javascript') nicholas@2222: else: nicholas@2222: s.send_header("Content-type", 'application/octet-stream') nicholas@2222: s.send_header("Content-Length", size) nicholas@2222: s.end_headers() nicholas@2222: s.wfile.write(fileDump.read()) nicholas@2222: fileDump.close() nicholas@2222: elif sys.version_info[0] == 3: nicholas@2222: s.path = s.path.rsplit('?') nicholas@2222: s.path = s.path[0] nicholas@2222: s.path = s.path[1:len(s.path)] nicholas@2222: st = s.path.rsplit(',') nicholas@2222: lenSt = len(st) nicholas@2222: fmt = st[lenSt-1].rsplit('.') nicholas@2230: fpath = "../"+urllib2.parse.unquote(s.path) nicholas@2222: s.send_response(200) nicholas@2222: if (fmt[1] == 'html'): nicholas@2222: s.send_header("Content-type", 'text/html') nicholas@2222: fileDump = open(fpath, encoding='utf-8') nicholas@2222: fileBytes = bytes(fileDump.read(), "utf-8") nicholas@2222: fileDump.close() nicholas@2222: elif (fmt[1] == 'css'): nicholas@2222: s.send_header("Content-type", 'text/css') nicholas@2222: fileDump = open(fpath, encoding='utf-8') nicholas@2222: fileBytes = bytes(fileDump.read(), "utf-8") nicholas@2222: fileDump.close() nicholas@2222: elif (fmt[1] == 'js'): nicholas@2222: s.send_header("Content-type", 'application/javascript') nicholas@2222: fileDump = open(fpath, encoding='utf-8') nicholas@2222: fileBytes = bytes(fileDump.read(), "utf-8") nicholas@2222: fileDump.close() nicholas@2222: else: nicholas@2222: s.send_header("Content-type", 'application/octet-stream') nicholas@2222: fileDump = open(fpath, 'rb') nicholas@2222: fileBytes = fileDump.read() nicholas@2222: fileDump.close() nicholas@2222: s.send_header("Content-Length", len(fileBytes)) nicholas@2222: s.end_headers() nicholas@2222: s.wfile.write(fileBytes) b@2211: b@2211: def keygen(s): b@2211: reply = "" b@2211: options = s.path.rsplit('?') b@2211: options = options[1].rsplit('=') b@2211: key = options[1] nicholas@2230: print("Registered key "+key) b@2211: if os.path.isfile("saves/save-"+key+".xml"): b@2211: reply = "NO"+key+"" b@2211: else: b@2211: reply = "OK"+key+"" b@2211: s.send_response(200) b@2211: s.send_header("Content-type", "application/xml") b@2211: s.end_headers() b@2211: s.wfile.write(reply) nicholas@2222: file = open("../saves/save-"+key+".xml",'w') b@2211: file.write("") b@2211: file.close(); b@2211: b@2211: def saveFile(self): nicholas@2222: global curFileName nicholas@2222: global curSaveIndex nicholas@2222: options = self.path.rsplit('?') nicholas@2222: options = options[1].rsplit('=') nicholas@2222: key = options[1] nicholas@2222: varLen = int(self.headers['Content-Length']) nicholas@2222: postVars = self.rfile.read(varLen) nicholas@2230: print("Saving file key "+key) nicholas@2222: file = open('../saves/save-'+key+'.xml','w') nicholas@2222: file.write(postVars) nicholas@2222: file.close() nicholas@2222: try: nicholas@2222: wbytes = os.path.getsize('../saves/save-'+key+'.xml') nicholas@2222: except OSError: nicholas@2222: self.send_response(200) nicholas@2222: self.send_header("Content-type", "text/xml") nicholas@2222: self.end_headers() nicholas@2222: self.wfile.write('Could not open file') nicholas@2222: self.send_response(200) nicholas@2222: self.send_header("Content-type", "text/xml") nicholas@2222: self.end_headers() nicholas@2222: self.wfile.write('OK"saves/'+curFileName+'"') nicholas@2222: curSaveIndex += 1 nicholas@2222: curFileName = 'test-'+str(curSaveIndex)+'.xml' b@2211: nicholas@2230: def http_do_HEAD(s): nicholas@2230: s.send_response(200) nicholas@2230: s.send_header("Content-type", "text/html") nicholas@2230: s.end_headers() b@2211: nicholas@2230: def http_do_GET(request): nicholas@2230: if(request.client_address[0] == "127.0.0.1"): nicholas@2230: if (request.path == "/favicon.ico"): nicholas@2230: send404(request) nicholas@2237: elif (request.path.split('?',1)[0] == "/php/keygen.php"): nicholas@2230: keygen(request); nicholas@2230: else: nicholas@2230: request.path = request.path.split('?',1)[0] nicholas@2230: if (request.path == '/'): nicholas@2230: request.path = '/index.html' nicholas@2230: elif (request.path == '/pseudo.xml'): nicholas@2230: request.path = '/'+PSEUDO_PATH + pseudo_files[pseudo_index] nicholas@2230: print(request.path) nicholas@2230: pseudo_index += 1 nicholas@2230: pseudo_index %= len(pseudo_files) nicholas@2230: processFile(request) nicholas@2230: else: nicholas@2230: send404(request) nicholas@2230: nicholas@2230: def http_do_POST(request): nicholas@2230: if(request.client_address[0] == "127.0.0.1"): nicholas@2237: if (request.path.rsplit('?',1)[0] == "/save" or request.path.rsplit('?',1)[0] == "/php/save.php"): nicholas@2230: saveFile(request) nicholas@2230: else: nicholas@2230: send404(request) nicholas@2230: nicholas@2222: if sys.version_info[0] == 2: nicholas@2230: class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler): nicholas@2230: def do_HEAD(s): nicholas@2230: http_do_HEAD(s) nicholas@2230: def do_GET(request): nicholas@2230: http_do_GET(request) nicholas@2230: def do_POST(request): nicholas@2230: http_do_POST(request) nicholas@2222: def run(server_class=BaseHTTPServer.HTTPServer,handler_class=MyHandler): nicholas@2222: server_address = ('', 8000) nicholas@2222: httpd = server_class(server_address, handler_class) nicholas@2222: httpd.serve_forever() nicholas@2230: run() nicholas@2222: elif sys.version_info[0] == 3: nicholas@2230: class MyHandler(BaseHTTPRequestHandler): nicholas@2230: def do_HEAD(s): nicholas@2230: send404(s) nicholas@2230: def do_GET(request): nicholas@2230: http_do_GET(request) nicholas@2230: def do_POST(request): nicholas@2230: http_do_POST(request) nicholas@2222: def run(server_class=HTTPServer,handler_class=MyHandler): nicholas@2222: server_address = ('', 8000) nicholas@2222: httpd = server_class(server_address, handler_class) nicholas@2222: httpd.serve_forever() nicholas@2230: run()