nickjillings@1626: import BaseHTTPServer nickjillings@1626: from os import walk nickjillings@1626: from os import path nickjillings@1626: import urllib2 nickjillings@1627: import pickle nickjillings@1627: import datetime nickjillings@1627: nickjillings@1627: curSaveIndex = 0; nickjillings@1627: curFileName = 'test-0.xml' nickjillings@1627: while(path.isfile('saves/'+curFileName)): nickjillings@1627: curSaveIndex += 1; nickjillings@1627: curFileName = 'test-'+str(curSaveIndex)+'.xml' nickjillings@1627: nickjillings@1627: print curFileName nickjillings@1626: nickjillings@1626: def send404(s): nickjillings@1626: s.send_response(404) nickjillings@1626: s.send_header("Content-type", "text/html") nickjillings@1626: s.end_headers() nickjillings@1626: nickjillings@1626: def processFile(s): nickjillings@1626: s.path = s.path[1:len(s.path)] nickjillings@1626: st = s.path.rsplit(',') nickjillings@1626: lenSt = len(st) nickjillings@1626: fmt = st[lenSt-1].rsplit('.') nickjillings@1626: size = path.getsize(urllib2.unquote(s.path)) nickjillings@1626: fileDump = open(urllib2.unquote(s.path)) nickjillings@1626: s.send_response(200) nickjillings@1626: nickjillings@1626: if (fmt[1] == 'html'): nickjillings@1626: s.send_header("Content-type", 'text/html') nickjillings@1626: elif (fmt[1] == 'css'): nickjillings@1626: s.send_header("Content-type", 'text/css') nickjillings@1626: elif (fmt[1] == 'js'): nickjillings@1626: s.send_header("Content-type", 'application/javascript') nickjillings@1626: else: nickjillings@1626: s.send_header("Content-type", 'application/octet-stream') nickjillings@1626: s.send_header("Content-Length", size) nickjillings@1626: s.end_headers() nickjillings@1626: s.wfile.write(fileDump.read()) nickjillings@1626: fileDump.close() nickjillings@1626: nickjillings@1627: def saveFile(self): nickjillings@1627: global curFileName nickjillings@1627: global curSaveIndex nickjillings@1627: varLen = int(self.headers['Content-Length']) nickjillings@1627: postVars = self.rfile.read(varLen) nickjillings@1627: print curFileName nickjillings@1627: file = open('saves/'+curFileName,'w') nickjillings@1627: curSaveIndex += 1; nickjillings@1627: curFileName = 'test-'+str(curSaveIndex)+'.xml' nickjillings@1627: print curFileName nickjillings@1627: file.write(postVars) nickjillings@1627: file.close() nickjillings@1627: self.send_response(200) nickjillings@1627: self.send_header("Content-type", "text/xml") nickjillings@1627: self.end_headers() nickjillings@1627: self.wfile.write('OKsaves/'+curFileName+'') nickjillings@1626: nickjillings@1626: class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler): nickjillings@1626: def do_HEAD(s): nickjillings@1626: s.send_response(200) nickjillings@1626: s.send_header("Content-type", "text/html") nickjillings@1626: s.end_headers() nickjillings@1626: def do_GET(request): nickjillings@1626: if(request.client_address[0] == "127.0.0.1"): nickjillings@1626: if (request.path == "/favicon.ico"): nickjillings@1626: send404(request) nickjillings@1626: else: nickjillings@1626: if (request.path == '/'): nickjillings@1626: request.path = '/index.html' nickjillings@1626: processFile(request) nickjillings@1626: else: nickjillings@1626: send404(request) nickjillings@1627: def do_POST(request): nickjillings@1627: if(request.client_address[0] == "127.0.0.1"): nickjillings@1627: if (request.path == "/save"): nickjillings@1627: saveFile(request) nickjillings@1627: else: nickjillings@1627: send404(request) nickjillings@1626: nickjillings@1626: def run(server_class=BaseHTTPServer.HTTPServer, nickjillings@1626: handler_class=MyHandler): nickjillings@1626: server_address = ('', 8000) nickjillings@1626: httpd = server_class(server_address, handler_class) nickjillings@1626: httpd.serve_forever() nickjillings@1626: nickjillings@1626: run()