annotate pythonServer-3.py @ 630:9dcfd654abad Dev_main

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