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