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