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