annotate python2.py @ 198:a7b377b86ed6

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