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