annotate pythonServer.py @ 1199:29eaafbe6248

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