annotate python2.py @ 957:96dc339fb382

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