annotate python/pythonServer.py @ 3028:9a201c63a0eb

Implementation for #231
author Nicholas Jillings <nicholas.jillings@mail.bcu.ac.uk>
date Tue, 19 Sep 2017 20:26:54 +0300
parents b09e9b7ef246
children b3230c0da598
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('.')
b@2264 72 fpath = "../"+urllib2.unquote(s.path)
n@2432 73 size = os.path.getsize(fpath)
b@2264 74 fileDump = open(fpath)
b@2264 75 s.send_response(200)
b@2264 76
b@2264 77 if (fmt[1] == 'html'):
b@2264 78 s.send_header("Content-type", 'text/html')
b@2264 79 elif (fmt[1] == 'css'):
b@2264 80 s.send_header("Content-type", 'text/css')
b@2264 81 elif (fmt[1] == 'js'):
b@2264 82 s.send_header("Content-type", 'application/javascript')
b@2264 83 else:
b@2264 84 s.send_header("Content-type", 'application/octet-stream')
nicholas@2542 85 fileRead = fileDump.read()
nicholas@2542 86 s.send_header("Content-Length", len(fileRead))
b@2264 87 s.end_headers()
nicholas@2542 88 s.wfile.write(fileRead)
b@2264 89 fileDump.close()
b@2264 90 elif sys.version_info[0] == 3:
b@2264 91 s.path = s.path.rsplit('?')
b@2264 92 s.path = s.path[0]
b@2264 93 s.path = s.path[1:len(s.path)]
b@2264 94 st = s.path.rsplit(',')
b@2264 95 lenSt = len(st)
b@2264 96 fmt = st[lenSt-1].rsplit('.')
b@2264 97 fpath = "../"+urllib2.parse.unquote(s.path)
b@2264 98 s.send_response(200)
b@2264 99 if (fmt[1] == 'html'):
b@2264 100 s.send_header("Content-type", 'text/html')
b@2264 101 fileDump = open(fpath, encoding='utf-8')
b@2264 102 fileBytes = bytes(fileDump.read(), "utf-8")
b@2264 103 fileDump.close()
b@2264 104 elif (fmt[1] == 'css'):
b@2264 105 s.send_header("Content-type", 'text/css')
b@2264 106 fileDump = open(fpath, encoding='utf-8')
b@2264 107 fileBytes = bytes(fileDump.read(), "utf-8")
b@2264 108 fileDump.close()
b@2264 109 elif (fmt[1] == 'js'):
b@2264 110 s.send_header("Content-type", 'application/javascript')
b@2264 111 fileDump = open(fpath, encoding='utf-8')
b@2264 112 fileBytes = bytes(fileDump.read(), "utf-8")
b@2264 113 fileDump.close()
b@2264 114 else:
b@2264 115 s.send_header("Content-type", 'application/octet-stream')
b@2264 116 fileDump = open(fpath, 'rb')
b@2264 117 fileBytes = fileDump.read()
b@2264 118 fileDump.close()
b@2264 119 s.send_header("Content-Length", len(fileBytes))
b@2264 120 s.end_headers()
b@2264 121 s.wfile.write(fileBytes)
nicholas@2510 122
nicholas@2510 123 def requestKey(s):
nicholas@2510 124 reply = ""
nicholas@2510 125 key = ''
nicholas@2510 126 while key == '':
nicholas@2510 127 tempKey = ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(32));
nicholas@2510 128 if (os.path.isfile("saves/save-"+tempKey+".xml") == False):
nicholas@2510 129 key = tempKey
nicholas@2940 130 options = s.path.rsplit('?')
nicholas@2940 131 options = options[1].rsplit('&')
nicholas@2940 132 for option in options:
nicholas@2940 133 optionPair = option.rsplit('=')
nicholas@2940 134 if optionPair[0] == "saveFilenamePrefix":
nicholas@2940 135 prefix = optionPair[1]
nicholas@2940 136 if prefix == None:
nicholas@2940 137 prefix = "save"
nicholas@2510 138 s.send_response(200)
nicholas@2510 139 s.send_header("Content-type", "application/xml");
nicholas@2510 140 s.end_headers()
nicholas@2510 141 reply = "<response><state>OK</state><key>"+key+"</key></response>"
nicholas@2510 142 if sys.version_info[0] == 2:
nicholas@2510 143 s.wfile.write(reply)
nicholas@2510 144 elif sys.version_info[0] == 3:
nicholas@2510 145 s.wfile.write(bytes(reply, "utf-8"))
nicholas@2940 146 file = open("../saves/"+prefix+"-"+key+".xml",'w')
nicholas@2932 147 file.write("<waetresult key=\""+key+"\"/>")
nicholas@2510 148 file.close()
nicholas@2510 149
b@2264 150
b@2264 151 def saveFile(self):
b@2264 152 global curFileName
b@2264 153 global curSaveIndex
b@2264 154 options = self.path.rsplit('?')
nicholas@2722 155 options = options[1].rsplit('&')
n@2997 156 update = False
nicholas@2722 157 for option in options:
nicholas@2722 158 optionPair = option.rsplit('=')
nicholas@2722 159 if optionPair[0] == "key":
nicholas@2722 160 key = optionPair[1]
nicholas@2722 161 elif optionPair[0] == "saveFilenamePrefix":
nicholas@2722 162 prefix = optionPair[1]
n@2997 163 elif optionPair[0] == "state":
n@2997 164 update = optionPair[1] == "update"
nicholas@2722 165 if key == None:
nicholas@2722 166 self.send_response(404)
nicholas@2722 167 return
nicholas@2722 168 if prefix == None:
nicholas@2722 169 prefix = "save"
b@2264 170 varLen = int(self.headers['Content-Length'])
b@2264 171 postVars = self.rfile.read(varLen)
b@2264 172 print("Saving file key "+key)
nicholas@2722 173 filename = prefix+'-'+key+'.xml'
n@2997 174 if update:
n@2997 175 filename = "update-"+filename
nicholas@2722 176 file = open('../saves/'+filename,'wb')
b@2264 177 file.write(postVars)
b@2264 178 file.close()
b@2264 179 try:
nicholas@2722 180 wbytes = os.path.getsize('../saves/'+filename)
b@2264 181 except OSError:
b@2264 182 self.send_response(200)
b@2264 183 self.send_header("Content-type", "text/xml")
b@2264 184 self.end_headers()
b@2264 185 self.wfile.write('<response state="error"><message>Could not open file</message></response>')
b@2264 186 self.send_response(200)
b@2264 187 self.send_header("Content-type", "text/xml")
b@2264 188 self.end_headers()
nicholas@2722 189 reply = '<response state="OK"><message>OK</message><file bytes="'+str(wbytes)+'">"saves/'+filename+'"</file></response>'
nicholas@2382 190 if sys.version_info[0] == 2:
nicholas@2382 191 self.wfile.write(reply)
nicholas@2382 192 elif sys.version_info[0] == 3:
nicholas@2382 193 self.wfile.write(bytes(reply, "utf-8"))
b@2264 194 curSaveIndex += 1
b@2264 195 curFileName = 'test-'+str(curSaveIndex)+'.xml'
n@2997 196 if update == False:
n@2997 197 os.remove("../saves/update-"+filename)
nicholas@2934 198
nicholas@2934 199 def testSave(self):
nicholas@2934 200 self.send_response(200)
nicholas@2934 201 self.send_header("Content-type", "text/xml")
nicholas@2934 202 self.end_headers()
nicholas@2934 203 filename = "../saves/test-save.xml"
nicholas@2934 204 file = open(filename,'wb')
nicholas@2934 205 if sys.version_info[0] == 2:
nicholas@2934 206 file.write("<xml></xml>")
nicholas@2934 207 elif sys.version_info[0] == 3:
nicholas@2934 208 file.write(bytes("<xml></xml>", "utf-8"))
nicholas@2934 209 file.close()
nicholas@2934 210 message = ""
nicholas@2934 211 try:
nicholas@2934 212 wbytes = os.path.getsize(filename)
nicholas@2934 213 except OSError:
nicholas@2934 214 message = '<response state="error"><message>Could not open file</message></response>';
nicholas@2934 215 if sys.version_info[0] == 2:
nicholas@2934 216 self.wfile.write(message)
nicholas@2934 217 elif sys.version_info[0] == 3:
nicholas@2934 218 self.wfile.write(bytes(message, "utf-8"))
nicholas@2934 219 return
nicholas@2934 220 os.remove(filename)
nicholas@2934 221 message = '<response state="OK"><message>OK</message></response>';
nicholas@2934 222 if sys.version_info[0] == 2:
nicholas@2934 223 self.wfile.write(message)
nicholas@2934 224 elif sys.version_info[0] == 3:
nicholas@2934 225 self.wfile.write(bytes(message, "utf-8"))
b@2264 226
nicholas@2430 227 def poolXML(s):
nicholas@2430 228 pool = ET.parse('../tests/pool.xml')
nicholas@2430 229 root = pool.getroot()
nicholas@2430 230 setupNode = root.find("setup");
nicholas@2430 231 poolSize = setupNode.get("poolSize",0);
nicholas@2430 232 if (poolSize == 0):
nicholas@2430 233 s.path = s.path.split("/php",1)[0]+"/tests/pool/xml"
nicholas@2430 234 processFile(s)
nicholas@2430 235 return
nicholas@2431 236 poolSize = int(poolSize)
nicholas@2430 237 # Set up the store will all the test page key nodes
nicholas@2430 238 pages = {};
nicholas@2430 239 for page in root.iter("page"):
nicholas@2430 240 id = page.get("id")
nicholas@2430 241 pages[id] = 0
nicholas@2430 242 # Read the saves and determine the completed pages
nicholas@2430 243 for filename in os.listdir("../saves/"):
nicholas@2430 244 if filename.endswith(".xml"):
nicholas@2430 245 save = ET.parse("../saves/"+filename)
nicholas@2430 246 save_root = save.getroot();
nicholas@2431 247 if (save_root.find("waet").get("url") == "http://localhost:8000/php/pool.php"):
nicholas@2431 248 for page in save_root.findall("./page"):
nicholas@2431 249 id = page.get("ref")
nicholas@2430 250 pages[id] = pages[id] + 1
nicholas@2430 251
nicholas@2430 252 # Sort the dictionary
nicholas@2431 253 rot_pages = {}
nicholas@2431 254 for key, value in pages.items():
nicholas@2431 255 if (value in rot_pages):
nicholas@2431 256 rot_pages[value].append(key)
nicholas@2431 257 else:
nicholas@2431 258 rot_pages[value] = [key]
nicholas@2431 259
nicholas@2431 260 Keys = list(rot_pages)
nicholas@2431 261 print ("Current pool state:")
nicholas@2431 262 print (rot_pages)
nicholas@2431 263
nicholas@2431 264 return_node = ET.fromstring('<waet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="test-schema.xsd"/>');
nicholas@2431 265 return_node.append(copy.deepcopy(root.find("setup")))
nicholas@2431 266 page_elements = root.findall("page")
nicholas@2430 267
nicholas@2431 268 # Now append the pages
nicholas@2431 269 i = 0
nicholas@2431 270 while(len(return_node.findall("page")) < poolSize):
nicholas@2431 271 if (i > 0):
nicholas@2431 272 for page in return_node.iter("page"):
nicholas@2431 273 page.set("alwaysInclude","true")
nicholas@2431 274 for id in rot_pages[Keys[i]]:
nicholas@2431 275 return_node.append(copy.deepcopy(root.find('./page[@id="'+id+'"]')))
nicholas@2431 276 i=i+1
nicholas@2431 277 s.send_response(200)
nicholas@2431 278 s.send_header("Content-type", "text/xml")
nicholas@2431 279 s.end_headers()
nicholas@2431 280 s.wfile.write(ET.tostring(return_node))
nicholas@2431 281
b@2264 282 def http_do_HEAD(s):
b@2264 283 s.send_response(200)
b@2264 284 s.send_header("Content-type", "text/html")
b@2264 285 s.end_headers()
b@2264 286
b@2264 287 def http_do_GET(request):
nicholas@2450 288 global pseudo_index
b@2264 289 if(request.client_address[0] == "127.0.0.1"):
b@2264 290 if (request.path == "/favicon.ico"):
b@2264 291 send404(request)
nicholas@2510 292 elif (request.path.split('?',1)[0] == "/php/requestKey.php"):
nicholas@2510 293 requestKey(request);
nicholas@2430 294 elif (request.path.split('?',1)[0] == "/php/pool.php"):
nicholas@2430 295 poolXML(request);
nicholas@2934 296 elif (request.path.split('?',1)[0] == "/php/test_write.php"):
nicholas@2934 297 testSave(request);
b@2264 298 else:
b@2264 299 request.path = request.path.split('?',1)[0]
b@2264 300 if (request.path == '/'):
b@2264 301 request.path = '/index.html'
b@2264 302 elif (request.path == '/pseudo.xml'):
nicholas@2450 303 request.path = PSEUDO_PATH + pseudo_files[pseudo_index]
b@2264 304 print(request.path)
b@2264 305 pseudo_index += 1
b@2264 306 pseudo_index %= len(pseudo_files)
b@2264 307 processFile(request)
b@2264 308 else:
b@2264 309 send404(request)
b@2264 310
b@2264 311 def http_do_POST(request):
b@2264 312 if(request.client_address[0] == "127.0.0.1"):
b@2264 313 if (request.path.rsplit('?',1)[0] == "/save" or request.path.rsplit('?',1)[0] == "/php/save.php"):
b@2264 314 saveFile(request)
b@2264 315 else:
b@2264 316 send404(request)
b@2264 317
b@2264 318 if sys.version_info[0] == 2:
b@2264 319 class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
b@2264 320 def do_HEAD(s):
b@2264 321 http_do_HEAD(s)
b@2264 322 def do_GET(request):
b@2264 323 http_do_GET(request)
b@2264 324 def do_POST(request):
b@2264 325 http_do_POST(request)
b@2264 326 def run(server_class=BaseHTTPServer.HTTPServer,handler_class=MyHandler):
b@2264 327 server_address = ('', 8000)
b@2264 328 httpd = server_class(server_address, handler_class)
b@2264 329 httpd.serve_forever()
b@2264 330 run()
b@2264 331 elif sys.version_info[0] == 3:
b@2264 332 class MyHandler(BaseHTTPRequestHandler):
b@2264 333 def do_HEAD(s):
b@2264 334 send404(s)
b@2264 335 def do_GET(request):
b@2264 336 http_do_GET(request)
b@2264 337 def do_POST(request):
b@2264 338 http_do_POST(request)
b@2264 339 def run(server_class=HTTPServer,handler_class=MyHandler):
b@2264 340 server_address = ('', 8000)
b@2264 341 httpd = server_class(server_address, handler_class)
b@2264 342 httpd.serve_forever()
b@2264 343 run()