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