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