annotate pythonServer.py @ 536:efac13499354 Dev_main

Merge into dev_main
author Nicholas Jillings <n.g.r.jillings@se14.qmul.ac.uk>
date Mon, 22 Feb 2016 12:19:43 +0000
parents 9c9fd68693b1
children 5b125fb39142
rev   line source
d@403 1 #!/usr/bin/python
d@403 2
b@242 3 import BaseHTTPServer
b@242 4 from os import walk
b@242 5 from os import path
nicholas@244 6 from os import listdir
b@259 7 import inspect
b@259 8 import os
b@242 9 import urllib2
b@242 10 import pickle
b@242 11 import datetime
nicholas@10 12
b@259 13 # Go to right folder.
b@259 14 scriptdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) # script directory
b@259 15 os.chdir(scriptdir) # does this work?
b@259 16
nicholas@244 17 PSEUDO_PATH = 'example_eval/'
nicholas@244 18 pseudo_files = []
nicholas@244 19 for filename in listdir(PSEUDO_PATH):
nicholas@244 20 if filename.endswith('.xml'):
nicholas@244 21 pseudo_files.append(filename)
nicholas@244 22
b@242 23 curSaveIndex = 0;
b@242 24 curFileName = 'test-0.xml'
b@242 25 while(path.isfile('saves/'+curFileName)):
b@242 26 curSaveIndex += 1;
b@242 27 curFileName = 'test-'+str(curSaveIndex)+'.xml'
nicholas@10 28
nicholas@244 29 pseudo_index = curSaveIndex % len(pseudo_files)
nicholas@436 30
nicholas@436 31 print 'URL: http://localhost:8000/index.html'
nicholas@10 32
b@242 33 def send404(s):
b@242 34 s.send_response(404)
b@242 35 s.send_header("Content-type", "text/html")
b@242 36 s.end_headers()
b@242 37
b@242 38 def processFile(s):
nicholas@436 39 s.path = s.path.rsplit('?')
nicholas@436 40 s.path = s.path[0]
b@242 41 s.path = s.path[1:len(s.path)]
b@242 42 st = s.path.rsplit(',')
b@242 43 lenSt = len(st)
b@242 44 fmt = st[lenSt-1].rsplit('.')
b@242 45 size = path.getsize(urllib2.unquote(s.path))
b@242 46 fileDump = open(urllib2.unquote(s.path))
b@242 47 s.send_response(200)
b@242 48
b@242 49 if (fmt[1] == 'html'):
b@242 50 s.send_header("Content-type", 'text/html')
b@242 51 elif (fmt[1] == 'css'):
b@242 52 s.send_header("Content-type", 'text/css')
b@242 53 elif (fmt[1] == 'js'):
b@242 54 s.send_header("Content-type", 'application/javascript')
b@242 55 else:
b@242 56 s.send_header("Content-type", 'application/octet-stream')
b@242 57 s.send_header("Content-Length", size)
b@242 58 s.end_headers()
b@242 59 s.wfile.write(fileDump.read())
b@242 60 fileDump.close()
b@242 61
b@242 62 def saveFile(self):
b@242 63 global curFileName
b@242 64 global curSaveIndex
b@242 65 varLen = int(self.headers['Content-Length'])
b@242 66 postVars = self.rfile.read(varLen)
b@242 67 print curFileName
b@242 68 file = open('saves/'+curFileName,'w')
b@242 69 file.write(postVars)
b@242 70 file.close()
nicholas@361 71 try:
nicholas@361 72 wbytes = os.path.getsize('saves/'+curFileName)
nicholas@361 73 except OSError:
nicholas@361 74 self.send_response(200)
nicholas@361 75 self.send_header("Content-type", "text/xml")
nicholas@361 76 self.end_headers()
nicholas@361 77 self.wfile.write('<response state="error"><message>Could not open file</message></response>')
b@242 78 self.send_response(200)
b@242 79 self.send_header("Content-type", "text/xml")
b@242 80 self.end_headers()
nicholas@361 81 self.wfile.write('<response state="OK"><message>OK</message><file bytes="'+str(wbytes)+'">"saves/'+curFileName+'"</file></response>')
nicholas@361 82 curSaveIndex += 1
nicholas@361 83 curFileName = 'test-'+str(curSaveIndex)+'.xml'
nicholas@10 84
b@242 85 class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
b@242 86 def do_HEAD(s):
nicholas@244 87 s.send_response(200)
nicholas@244 88 s.send_header("Content-type", "text/html")
nicholas@244 89 s.end_headers()
b@242 90 def do_GET(request):
nicholas@244 91 global pseudo_index
nicholas@244 92 global pseudo_files
nicholas@244 93 global PSEUDO_PATH
b@242 94 if(request.client_address[0] == "127.0.0.1"):
b@242 95 if (request.path == "/favicon.ico"):
b@242 96 send404(request)
b@242 97 else:
b@242 98 if (request.path == '/'):
b@242 99 request.path = '/index.html'
nicholas@244 100 elif (request.path == '/pseudo.xml'):
nicholas@244 101 request.path = '/'+PSEUDO_PATH + pseudo_files[pseudo_index]
nicholas@244 102 print request.path
nicholas@244 103 pseudo_index += 1
nicholas@244 104 pseudo_index %= len(pseudo_files)
b@242 105 processFile(request)
b@242 106 else:
b@242 107 send404(request)
nicholas@244 108
b@242 109 def do_POST(request):
b@242 110 if(request.client_address[0] == "127.0.0.1"):
nicholas@361 111 if (request.path == "/save" or request.path == "/save.php"):
b@242 112 saveFile(request)
b@242 113 else:
b@242 114 send404(request)
b@242 115
b@242 116 def run(server_class=BaseHTTPServer.HTTPServer,
b@242 117 handler_class=MyHandler):
b@242 118 server_address = ('', 8000)
b@242 119 httpd = server_class(server_address, handler_class)
b@242 120 httpd.serve_forever()
b@242 121
b@242 122 run()