annotate pythonServer-3.py @ 1232:bf8716341ed6

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