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