annotate python/pythonServer.py @ 3065:b3230c0da598

Possible fix for #196
author Nicholas Jillings <nicholas.jillings@mail.bcu.ac.uk>
date Tue, 31 Oct 2017 17:14:23 +0000
parents 9a201c63a0eb
children aae564b49b5a
rev   line source
b@2264 1 #!/usr/bin/python
b@2264 2
b@2264 3 # Detect the Python version to switch code between 2.x and 3.x
b@2264 4 # http://stackoverflow.com/questions/9079036/detect-python-version-at-runtime
b@2264 5 import sys
b@2264 6
b@2264 7 import inspect
b@2264 8 import os
b@2264 9 import pickle
b@2264 10 import datetime
nicholas@2430 11 import operator
nicholas@2430 12 import xml.etree.ElementTree as ET
nicholas@2431 13 import copy
nicholas@2510 14 import string
nicholas@2510 15 import random
nicholas@3028 16 import errno
b@2264 17
b@2264 18 if sys.version_info[0] == 2:
b@2264 19 # Version 2.x
b@2264 20 import BaseHTTPServer
b@2264 21 import urllib2
b@2264 22 import urlparse
b@2264 23 elif sys.version_info[0] == 3:
b@2264 24 # Version 3.x
b@2264 25 from http.server import BaseHTTPRequestHandler, HTTPServer
b@2264 26 import urllib as urllib2
b@2264 27
b@2264 28 # Go to right folder.
b@2264 29 scriptdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) # script directory
b@2264 30 os.chdir(scriptdir) # does this work?
b@2264 31
nicholas@3028 32 try:
nicholas@3028 33 os.makedirs("../saves")
nicholas@3028 34 except OSError as e:
nicholas@3028 35 if e.errno != errno.EEXIST:
nicholas@3028 36 raise
nicholas@3028 37
b@2264 38 PSEUDO_PATH = '../tests/'
b@2264 39 pseudo_files = []
nicholas@2450 40 pseudo_index = 0
nicholas@2430 41 for filename in os.listdir(PSEUDO_PATH):
b@2264 42 print(filename)
b@2264 43 if filename.endswith('.xml'):
b@2264 44 pseudo_files.append(filename)
b@2264 45
b@2264 46 curSaveIndex = 0;
b@2264 47 curFileName = 'test-0.xml'
nicholas@2430 48 while(os.path.isfile('../saves/'+curFileName)):
b@2264 49 curSaveIndex += 1;
b@2264 50 curFileName = 'test-'+str(curSaveIndex)+'.xml'
b@2264 51
b@2264 52 if len(pseudo_files) > 0:
b@2264 53 pseudo_index = curSaveIndex % len(pseudo_files)
b@2264 54 else:
b@2264 55 pseudo_index = 0
b@2264 56
b@2264 57 print('URL: http://localhost:8000/index.html')
b@2264 58
b@2264 59 def send404(s):
b@2264 60 s.send_response(404)
b@2264 61 s.send_header("Content-type", "text/html")
b@2264 62 s.end_headers()
b@2264 63
b@2264 64 def processFile(s):
b@2264 65 if sys.version_info[0] == 2:
b@2264 66 s.path = s.path.rsplit('?')
b@2264 67 s.path = s.path[0]
b@2264 68 s.path = s.path[1:len(s.path)]
b@2264 69 st = s.path.rsplit(',')
b@2264 70 lenSt = len(st)
b@2264 71 fmt = st[lenSt-1].rsplit('.')
nicholas@3065 72 fmt = fmt[len(fmt)-1]
nicholas@3065 73 print(st)
nicholas@3065 74 print(fmt)
b@2264 75 fpath = "../"+urllib2.unquote(s.path)
n@2432 76 size = os.path.getsize(fpath)
nicholas@3065 77 fileDump = open(fpath, mode='rb')
b@2264 78 s.send_response(200)
b@2264 79
nicholas@3065 80 if (fmt == 'html'):
b@2264 81 s.send_header("Content-type", 'text/html')
nicholas@3065 82 elif (fmt == 'css'):
b@2264 83 s.send_header("Content-type", 'text/css')
nicholas@3065 84 elif (fmt == 'js'):
b@2264 85 s.send_header("Content-type", 'application/javascript')
b@2264 86 else:
b@2264 87 s.send_header("Content-type", 'application/octet-stream')
nicholas@2542 88 fileRead = fileDump.read()
nicholas@2542 89 s.send_header("Content-Length", len(fileRead))
b@2264 90 s.end_headers()
nicholas@2542 91 s.wfile.write(fileRead)
b@2264 92 fileDump.close()
b@2264 93 elif sys.version_info[0] == 3:
b@2264 94 s.path = s.path.rsplit('?')
b@2264 95 s.path = s.path[0]
b@2264 96 s.path = s.path[1:len(s.path)]
b@2264 97 st = s.path.rsplit(',')
b@2264 98 lenSt = len(st)
b@2264 99 fmt = st[lenSt-1].rsplit('.')
b@2264 100 fpath = "../"+urllib2.parse.unquote(s.path)
b@2264 101 s.send_response(200)
b@2264 102 if (fmt[1] == 'html'):
b@2264 103 s.send_header("Content-type", 'text/html')
b@2264 104 fileDump = open(fpath, encoding='utf-8')
b@2264 105 fileBytes = bytes(fileDump.read(), "utf-8")
b@2264 106 fileDump.close()
b@2264 107 elif (fmt[1] == 'css'):
b@2264 108 s.send_header("Content-type", 'text/css')
b@2264 109 fileDump = open(fpath, encoding='utf-8')
b@2264 110 fileBytes = bytes(fileDump.read(), "utf-8")
b@2264 111 fileDump.close()
b@2264 112 elif (fmt[1] == 'js'):
b@2264 113 s.send_header("Content-type", 'application/javascript')
b@2264 114 fileDump = open(fpath, encoding='utf-8')
b@2264 115 fileBytes = bytes(fileDump.read(), "utf-8")
b@2264 116 fileDump.close()
b@2264 117 else:
b@2264 118 s.send_header("Content-type", 'application/octet-stream')
b@2264 119 fileDump = open(fpath, 'rb')
b@2264 120 fileBytes = fileDump.read()
b@2264 121 fileDump.close()
b@2264 122 s.send_header("Content-Length", len(fileBytes))
b@2264 123 s.end_headers()
b@2264 124 s.wfile.write(fileBytes)
nicholas@2510 125
nicholas@2510 126 def requestKey(s):
nicholas@2510 127 reply = ""
nicholas@2510 128 key = ''
nicholas@2510 129 while key == '':
nicholas@2510 130 tempKey = ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(32));
nicholas@2510 131 if (os.path.isfile("saves/save-"+tempKey+".xml") == False):
nicholas@2510 132 key = tempKey
nicholas@2940 133 options = s.path.rsplit('?')
nicholas@2940 134 options = options[1].rsplit('&')
nicholas@2940 135 for option in options:
nicholas@2940 136 optionPair = option.rsplit('=')
nicholas@2940 137 if optionPair[0] == "saveFilenamePrefix":
nicholas@2940 138 prefix = optionPair[1]
nicholas@2940 139 if prefix == None:
nicholas@2940 140 prefix = "save"
nicholas@2510 141 s.send_response(200)
nicholas@2510 142 s.send_header("Content-type", "application/xml");
nicholas@2510 143 s.end_headers()
nicholas@2510 144 reply = "<response><state>OK</state><key>"+key+"</key></response>"
nicholas@2510 145 if sys.version_info[0] == 2:
nicholas@2510 146 s.wfile.write(reply)
nicholas@2510 147 elif sys.version_info[0] == 3:
nicholas@2510 148 s.wfile.write(bytes(reply, "utf-8"))
nicholas@2940 149 file = open("../saves/"+prefix+"-"+key+".xml",'w')
nicholas@2932 150 file.write("<waetresult key=\""+key+"\"/>")
nicholas@2510 151 file.close()
nicholas@2510 152
b@2264 153
b@2264 154 def saveFile(self):
b@2264 155 global curFileName
b@2264 156 global curSaveIndex
b@2264 157 options = self.path.rsplit('?')
nicholas@2722 158 options = options[1].rsplit('&')
n@2997 159 update = False
nicholas@2722 160 for option in options:
nicholas@2722 161 optionPair = option.rsplit('=')
nicholas@2722 162 if optionPair[0] == "key":
nicholas@2722 163 key = optionPair[1]
nicholas@2722 164 elif optionPair[0] == "saveFilenamePrefix":
nicholas@2722 165 prefix = optionPair[1]
n@2997 166 elif optionPair[0] == "state":
n@2997 167 update = optionPair[1] == "update"
nicholas@2722 168 if key == None:
nicholas@2722 169 self.send_response(404)
nicholas@2722 170 return
nicholas@2722 171 if prefix == None:
nicholas@2722 172 prefix = "save"
b@2264 173 varLen = int(self.headers['Content-Length'])
b@2264 174 postVars = self.rfile.read(varLen)
b@2264 175 print("Saving file key "+key)
nicholas@2722 176 filename = prefix+'-'+key+'.xml'
n@2997 177 if update:
n@2997 178 filename = "update-"+filename
nicholas@2722 179 file = open('../saves/'+filename,'wb')
b@2264 180 file.write(postVars)
b@2264 181 file.close()
b@2264 182 try:
nicholas@2722 183 wbytes = os.path.getsize('../saves/'+filename)
b@2264 184 except OSError:
b@2264 185 self.send_response(200)
b@2264 186 self.send_header("Content-type", "text/xml")
b@2264 187 self.end_headers()
b@2264 188 self.wfile.write('<response state="error"><message>Could not open file</message></response>')
b@2264 189 self.send_response(200)
b@2264 190 self.send_header("Content-type", "text/xml")
b@2264 191 self.end_headers()
nicholas@2722 192 reply = '<response state="OK"><message>OK</message><file bytes="'+str(wbytes)+'">"saves/'+filename+'"</file></response>'
nicholas@2382 193 if sys.version_info[0] == 2:
nicholas@2382 194 self.wfile.write(reply)
nicholas@2382 195 elif sys.version_info[0] == 3:
nicholas@2382 196 self.wfile.write(bytes(reply, "utf-8"))
b@2264 197 curSaveIndex += 1
b@2264 198 curFileName = 'test-'+str(curSaveIndex)+'.xml'
n@2997 199 if update == False:
n@2997 200 os.remove("../saves/update-"+filename)
nicholas@2934 201
nicholas@2934 202 def testSave(self):
nicholas@2934 203 self.send_response(200)
nicholas@2934 204 self.send_header("Content-type", "text/xml")
nicholas@2934 205 self.end_headers()
nicholas@2934 206 filename = "../saves/test-save.xml"
nicholas@2934 207 file = open(filename,'wb')
nicholas@2934 208 if sys.version_info[0] == 2:
nicholas@2934 209 file.write("<xml></xml>")
nicholas@2934 210 elif sys.version_info[0] == 3:
nicholas@2934 211 file.write(bytes("<xml></xml>", "utf-8"))
nicholas@2934 212 file.close()
nicholas@2934 213 message = ""
nicholas@2934 214 try:
nicholas@2934 215 wbytes = os.path.getsize(filename)
nicholas@2934 216 except OSError:
nicholas@2934 217 message = '<response state="error"><message>Could not open file</message></response>';
nicholas@2934 218 if sys.version_info[0] == 2:
nicholas@2934 219 self.wfile.write(message)
nicholas@2934 220 elif sys.version_info[0] == 3:
nicholas@2934 221 self.wfile.write(bytes(message, "utf-8"))
nicholas@2934 222 return
nicholas@2934 223 os.remove(filename)
nicholas@2934 224 message = '<response state="OK"><message>OK</message></response>';
nicholas@2934 225 if sys.version_info[0] == 2:
nicholas@2934 226 self.wfile.write(message)
nicholas@2934 227 elif sys.version_info[0] == 3:
nicholas@2934 228 self.wfile.write(bytes(message, "utf-8"))
b@2264 229
nicholas@2430 230 def poolXML(s):
nicholas@2430 231 pool = ET.parse('../tests/pool.xml')
nicholas@2430 232 root = pool.getroot()
nicholas@2430 233 setupNode = root.find("setup");
nicholas@2430 234 poolSize = setupNode.get("poolSize",0);
nicholas@2430 235 if (poolSize == 0):
nicholas@2430 236 s.path = s.path.split("/php",1)[0]+"/tests/pool/xml"
nicholas@2430 237 processFile(s)
nicholas@2430 238 return
nicholas@2431 239 poolSize = int(poolSize)
nicholas@2430 240 # Set up the store will all the test page key nodes
nicholas@2430 241 pages = {};
nicholas@2430 242 for page in root.iter("page"):
nicholas@2430 243 id = page.get("id")
nicholas@2430 244 pages[id] = 0
nicholas@2430 245 # Read the saves and determine the completed pages
nicholas@2430 246 for filename in os.listdir("../saves/"):
nicholas@2430 247 if filename.endswith(".xml"):
nicholas@2430 248 save = ET.parse("../saves/"+filename)
nicholas@2430 249 save_root = save.getroot();
nicholas@2431 250 if (save_root.find("waet").get("url") == "http://localhost:8000/php/pool.php"):
nicholas@2431 251 for page in save_root.findall("./page"):
nicholas@2431 252 id = page.get("ref")
nicholas@2430 253 pages[id] = pages[id] + 1
nicholas@2430 254
nicholas@2430 255 # Sort the dictionary
nicholas@2431 256 rot_pages = {}
nicholas@2431 257 for key, value in pages.items():
nicholas@2431 258 if (value in rot_pages):
nicholas@2431 259 rot_pages[value].append(key)
nicholas@2431 260 else:
nicholas@2431 261 rot_pages[value] = [key]
nicholas@2431 262
nicholas@2431 263 Keys = list(rot_pages)
nicholas@2431 264 print ("Current pool state:")
nicholas@2431 265 print (rot_pages)
nicholas@2431 266
nicholas@2431 267 return_node = ET.fromstring('<waet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="test-schema.xsd"/>');
nicholas@2431 268 return_node.append(copy.deepcopy(root.find("setup")))
nicholas@2431 269 page_elements = root.findall("page")
nicholas@2430 270
nicholas@2431 271 # Now append the pages
nicholas@2431 272 i = 0
nicholas@2431 273 while(len(return_node.findall("page")) < poolSize):
nicholas@2431 274 if (i > 0):
nicholas@2431 275 for page in return_node.iter("page"):
nicholas@2431 276 page.set("alwaysInclude","true")
nicholas@2431 277 for id in rot_pages[Keys[i]]:
nicholas@2431 278 return_node.append(copy.deepcopy(root.find('./page[@id="'+id+'"]')))
nicholas@2431 279 i=i+1
nicholas@2431 280 s.send_response(200)
nicholas@2431 281 s.send_header("Content-type", "text/xml")
nicholas@2431 282 s.end_headers()
nicholas@2431 283 s.wfile.write(ET.tostring(return_node))
nicholas@2431 284
b@2264 285 def http_do_HEAD(s):
b@2264 286 s.send_response(200)
b@2264 287 s.send_header("Content-type", "text/html")
b@2264 288 s.end_headers()
b@2264 289
b@2264 290 def http_do_GET(request):
nicholas@2450 291 global pseudo_index
b@2264 292 if(request.client_address[0] == "127.0.0.1"):
b@2264 293 if (request.path == "/favicon.ico"):
b@2264 294 send404(request)
nicholas@2510 295 elif (request.path.split('?',1)[0] == "/php/requestKey.php"):
nicholas@2510 296 requestKey(request);
nicholas@2430 297 elif (request.path.split('?',1)[0] == "/php/pool.php"):
nicholas@2430 298 poolXML(request);
nicholas@2934 299 elif (request.path.split('?',1)[0] == "/php/test_write.php"):
nicholas@2934 300 testSave(request);
b@2264 301 else:
b@2264 302 request.path = request.path.split('?',1)[0]
b@2264 303 if (request.path == '/'):
b@2264 304 request.path = '/index.html'
b@2264 305 elif (request.path == '/pseudo.xml'):
nicholas@2450 306 request.path = PSEUDO_PATH + pseudo_files[pseudo_index]
b@2264 307 print(request.path)
b@2264 308 pseudo_index += 1
b@2264 309 pseudo_index %= len(pseudo_files)
b@2264 310 processFile(request)
b@2264 311 else:
b@2264 312 send404(request)
b@2264 313
b@2264 314 def http_do_POST(request):
b@2264 315 if(request.client_address[0] == "127.0.0.1"):
b@2264 316 if (request.path.rsplit('?',1)[0] == "/save" or request.path.rsplit('?',1)[0] == "/php/save.php"):
b@2264 317 saveFile(request)
b@2264 318 else:
b@2264 319 send404(request)
b@2264 320
b@2264 321 if sys.version_info[0] == 2:
b@2264 322 class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
b@2264 323 def do_HEAD(s):
b@2264 324 http_do_HEAD(s)
b@2264 325 def do_GET(request):
b@2264 326 http_do_GET(request)
b@2264 327 def do_POST(request):
b@2264 328 http_do_POST(request)
b@2264 329 def run(server_class=BaseHTTPServer.HTTPServer,handler_class=MyHandler):
b@2264 330 server_address = ('', 8000)
b@2264 331 httpd = server_class(server_address, handler_class)
b@2264 332 httpd.serve_forever()
b@2264 333 run()
b@2264 334 elif sys.version_info[0] == 3:
b@2264 335 class MyHandler(BaseHTTPRequestHandler):
b@2264 336 def do_HEAD(s):
b@2264 337 send404(s)
b@2264 338 def do_GET(request):
b@2264 339 http_do_GET(request)
b@2264 340 def do_POST(request):
b@2264 341 http_do_POST(request)
b@2264 342 def run(server_class=HTTPServer,handler_class=MyHandler):
b@2264 343 server_address = ('', 8000)
b@2264 344 httpd = server_class(server_address, handler_class)
b@2264 345 httpd.serve_forever()
b@2264 346 run()