annotate python2.py @ 923:7a7a72880996

Merge from the default branch
author Nicholas Jillings <n.g.r.jillings@se14.qmul.ac.uk>
date Tue, 02 Jun 2015 11:24:23 +0100
parents 533d51508e93
children 2dc61bd6494e
rev   line source
n@921 1 import BaseHTTPServer
n@921 2 from os import walk
n@921 3 from os import path
n@921 4 import urllib2
n@921 5 import pickle
n@921 6 import datetime
n@921 7
n@921 8 curSaveIndex = 0;
n@921 9 curFileName = 'test-0.xml'
n@921 10 while(path.isfile('saves/'+curFileName)):
n@921 11 curSaveIndex += 1;
n@921 12 curFileName = 'test-'+str(curSaveIndex)+'.xml'
n@921 13
n@921 14 print curFileName
n@921 15
n@921 16 def send404(s):
n@921 17 s.send_response(404)
n@921 18 s.send_header("Content-type", "text/html")
n@921 19 s.end_headers()
n@921 20
n@921 21 def processFile(s):
n@921 22 s.path = s.path[1:len(s.path)]
n@921 23 st = s.path.rsplit(',')
n@921 24 lenSt = len(st)
n@921 25 fmt = st[lenSt-1].rsplit('.')
n@921 26 size = path.getsize(urllib2.unquote(s.path))
n@921 27 fileDump = open(urllib2.unquote(s.path))
n@921 28 s.send_response(200)
n@921 29
n@921 30 if (fmt[1] == 'html'):
n@921 31 s.send_header("Content-type", 'text/html')
n@921 32 elif (fmt[1] == 'css'):
n@921 33 s.send_header("Content-type", 'text/css')
n@921 34 elif (fmt[1] == 'js'):
n@921 35 s.send_header("Content-type", 'application/javascript')
n@921 36 else:
n@921 37 s.send_header("Content-type", 'application/octet-stream')
n@921 38 s.send_header("Content-Length", size)
n@921 39 s.end_headers()
n@921 40 s.wfile.write(fileDump.read())
n@921 41 fileDump.close()
n@921 42
n@921 43 def saveFile(self):
n@921 44 global curFileName
n@921 45 global curSaveIndex
n@921 46 varLen = int(self.headers['Content-Length'])
n@921 47 postVars = self.rfile.read(varLen)
n@921 48 print curFileName
n@921 49 file = open('saves/'+curFileName,'w')
n@921 50 curSaveIndex += 1;
n@921 51 curFileName = 'test-'+str(curSaveIndex)+'.xml'
n@921 52 print curFileName
n@921 53 file.write(postVars)
n@921 54 file.close()
n@921 55 self.send_response(200)
n@921 56 self.send_header("Content-type", "text/xml")
n@921 57 self.end_headers()
n@921 58 self.wfile.write('<response><state>OK</state><file>saves/'+curFileName+'</file></response>')
n@921 59
n@921 60 class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
n@921 61 def do_HEAD(s):
n@921 62 s.send_response(200)
n@921 63 s.send_header("Content-type", "text/html")
n@921 64 s.end_headers()
n@921 65 def do_GET(request):
n@921 66 if(request.client_address[0] == "127.0.0.1"):
n@921 67 if (request.path == "/favicon.ico"):
n@921 68 send404(request)
n@921 69 else:
n@921 70 if (request.path == '/'):
n@921 71 request.path = '/index.html'
n@921 72 processFile(request)
n@921 73 else:
n@921 74 send404(request)
n@921 75 def do_POST(request):
n@921 76 if(request.client_address[0] == "127.0.0.1"):
n@921 77 if (request.path == "/save"):
n@921 78 saveFile(request)
n@921 79 else:
n@921 80 send404(request)
n@921 81
n@921 82 def run(server_class=BaseHTTPServer.HTTPServer,
n@921 83 handler_class=MyHandler):
n@921 84 server_address = ('', 8000)
n@921 85 httpd = server_class(server_address, handler_class)
n@921 86 httpd.serve_forever()
n@921 87
n@921 88 run()