annotate pythonServer-3.py @ 2212:279733b3b67e

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