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