annotate scripts/pythonServer.py @ 2222:4d1aa94202e3

Merged pythonServer-3 into unified pythonServer. Removed pythonServer-legacy. Updated pythonServer to work in scripts subdir.
author Nicholas Jillings <nicholas.jillings@mail.bcu.ac.uk>
date Thu, 14 Apr 2016 13:19:17 +0100
parents 4615745ed30c
children 4e334c942d41
rev   line source
b@2211 1 #!/usr/bin/python
b@2211 2
nicholas@2222 3 # Detect the Python version to switch code between 2.x and 3.x
nicholas@2222 4 # http://stackoverflow.com/questions/9079036/detect-python-version-at-runtime
nicholas@2222 5 import sys
nicholas@2222 6
b@2211 7 from os import walk
b@2211 8 from os import path
b@2211 9 from os import listdir
b@2211 10 import inspect
b@2211 11 import os
b@2211 12 import pickle
b@2211 13 import datetime
b@2211 14
nicholas@2222 15 if sys.version_info[0] == 2:
nicholas@2222 16 # Version 2.x
nicholas@2222 17 import BaseHTTPServer
nicholas@2222 18 import urllib2
nicholas@2222 19 import urlparse
nicholas@2222 20 elif sys.version_info[0] == 3:
nicholas@2222 21 # Version 3.x
nicholas@2222 22 from http.server import BaseHTTPRequestHandler, HTTPServer
nicholas@2222 23 import urllib as urllib2
nicholas@2222 24
b@2211 25 # Go to right folder.
b@2211 26 scriptdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) # script directory
b@2211 27 os.chdir(scriptdir) # does this work?
b@2211 28
nicholas@2222 29 PSEUDO_PATH = '../example_eval/'
b@2211 30 pseudo_files = []
b@2211 31 for filename in listdir(PSEUDO_PATH):
b@2211 32 if filename.endswith('.xml'):
b@2211 33 pseudo_files.append(filename)
b@2211 34
b@2211 35 curSaveIndex = 0;
b@2211 36 curFileName = 'test-0.xml'
nicholas@2222 37 while(path.isfile('../saves/'+curFileName)):
nicholas@2222 38 curSaveIndex += 1;
nicholas@2222 39 curFileName = 'test-'+str(curSaveIndex)+'.xml'
b@2211 40
b@2211 41 pseudo_index = curSaveIndex % len(pseudo_files)
b@2211 42
nicholas@2222 43 if sys.version_info[0] == 2:
nicholas@2222 44 print 'URL: http://localhost:8000/index.html'
nicholas@2222 45 elif sys.version_info[0] == 3:
nicholas@2222 46 print('URL: http://localhost:8000/index.html')
b@2211 47
b@2211 48 def send404(s):
nicholas@2222 49 s.send_response(404)
nicholas@2222 50 s.send_header("Content-type", "text/html")
nicholas@2222 51 s.end_headers()
b@2211 52
b@2211 53 def processFile(s):
nicholas@2222 54 if sys.version_info[0] == 2:
nicholas@2222 55 s.path = s.path.rsplit('?')
nicholas@2222 56 s.path = s.path[0]
nicholas@2222 57 s.path = s.path[1:len(s.path)]
nicholas@2222 58 st = s.path.rsplit(',')
nicholas@2222 59 lenSt = len(st)
nicholas@2222 60 fmt = st[lenSt-1].rsplit('.')
nicholas@2222 61 fpath = "../"+urllib2.unquote(s.path)
nicholas@2222 62 size = path.getsize(fpath)
nicholas@2222 63 fileDump = open(fpath)
nicholas@2222 64 s.send_response(200)
nicholas@2222 65
nicholas@2222 66 if (fmt[1] == 'html'):
nicholas@2222 67 s.send_header("Content-type", 'text/html')
nicholas@2222 68 elif (fmt[1] == 'css'):
nicholas@2222 69 s.send_header("Content-type", 'text/css')
nicholas@2222 70 elif (fmt[1] == 'js'):
nicholas@2222 71 s.send_header("Content-type", 'application/javascript')
nicholas@2222 72 else:
nicholas@2222 73 s.send_header("Content-type", 'application/octet-stream')
nicholas@2222 74 s.send_header("Content-Length", size)
nicholas@2222 75 s.end_headers()
nicholas@2222 76 s.wfile.write(fileDump.read())
nicholas@2222 77 fileDump.close()
nicholas@2222 78 elif sys.version_info[0] == 3:
nicholas@2222 79 s.path = s.path.rsplit('?')
nicholas@2222 80 s.path = s.path[0]
nicholas@2222 81 s.path = s.path[1:len(s.path)]
nicholas@2222 82 st = s.path.rsplit(',')
nicholas@2222 83 lenSt = len(st)
nicholas@2222 84 fmt = st[lenSt-1].rsplit('.')
nicholas@2222 85 fpath = "../"+urllib2.unquote(s.path)
nicholas@2222 86 s.send_response(200)
nicholas@2222 87 if (fmt[1] == 'html'):
nicholas@2222 88 s.send_header("Content-type", 'text/html')
nicholas@2222 89 fileDump = open(fpath, encoding='utf-8')
nicholas@2222 90 fileBytes = bytes(fileDump.read(), "utf-8")
nicholas@2222 91 fileDump.close()
nicholas@2222 92 elif (fmt[1] == 'css'):
nicholas@2222 93 s.send_header("Content-type", 'text/css')
nicholas@2222 94 fileDump = open(fpath, encoding='utf-8')
nicholas@2222 95 fileBytes = bytes(fileDump.read(), "utf-8")
nicholas@2222 96 fileDump.close()
nicholas@2222 97 elif (fmt[1] == 'js'):
nicholas@2222 98 s.send_header("Content-type", 'application/javascript')
nicholas@2222 99 fileDump = open(fpath, encoding='utf-8')
nicholas@2222 100 fileBytes = bytes(fileDump.read(), "utf-8")
nicholas@2222 101 fileDump.close()
nicholas@2222 102 else:
nicholas@2222 103 s.send_header("Content-type", 'application/octet-stream')
nicholas@2222 104 fileDump = open(fpath, 'rb')
nicholas@2222 105 fileBytes = fileDump.read()
nicholas@2222 106 fileDump.close()
nicholas@2222 107 s.send_header("Content-Length", len(fileBytes))
nicholas@2222 108 s.end_headers()
nicholas@2222 109 s.wfile.write(fileBytes)
b@2211 110
b@2211 111 def keygen(s):
b@2211 112 reply = ""
b@2211 113 options = s.path.rsplit('?')
b@2211 114 options = options[1].rsplit('=')
b@2211 115 key = options[1]
b@2211 116 print key
b@2211 117 if os.path.isfile("saves/save-"+key+".xml"):
b@2211 118 reply = "<response><state>NO</state><key>"+key+"</key></response>"
b@2211 119 else:
b@2211 120 reply = "<response><state>OK</state><key>"+key+"</key></response>"
b@2211 121 s.send_response(200)
b@2211 122 s.send_header("Content-type", "application/xml")
b@2211 123 s.end_headers()
b@2211 124 s.wfile.write(reply)
nicholas@2222 125 file = open("../saves/save-"+key+".xml",'w')
b@2211 126 file.write("<waetresult key="+key+"/>")
b@2211 127 file.close();
b@2211 128
b@2211 129 def saveFile(self):
nicholas@2222 130 global curFileName
nicholas@2222 131 global curSaveIndex
nicholas@2222 132 options = self.path.rsplit('?')
nicholas@2222 133 options = options[1].rsplit('=')
nicholas@2222 134 key = options[1]
nicholas@2222 135 print key
nicholas@2222 136 varLen = int(self.headers['Content-Length'])
nicholas@2222 137 postVars = self.rfile.read(varLen)
nicholas@2222 138 if sys.version_info[0] == 2:
nicholas@2222 139 print "Saving file key "+key
nicholas@2222 140 elif sys.version_info[0] == 3:
nicholas@2222 141 print("Saving file key "+key)
nicholas@2222 142 file = open('../saves/save-'+key+'.xml','w')
nicholas@2222 143 file.write(postVars)
nicholas@2222 144 file.close()
nicholas@2222 145 try:
nicholas@2222 146 wbytes = os.path.getsize('../saves/save-'+key+'.xml')
nicholas@2222 147 except OSError:
nicholas@2222 148 self.send_response(200)
nicholas@2222 149 self.send_header("Content-type", "text/xml")
nicholas@2222 150 self.end_headers()
nicholas@2222 151 self.wfile.write('<response state="error"><message>Could not open file</message></response>')
nicholas@2222 152 self.send_response(200)
nicholas@2222 153 self.send_header("Content-type", "text/xml")
nicholas@2222 154 self.end_headers()
nicholas@2222 155 self.wfile.write('<response state="OK"><message>OK</message><file bytes="'+str(wbytes)+'">"saves/'+curFileName+'"</file></response>')
nicholas@2222 156 curSaveIndex += 1
nicholas@2222 157 curFileName = 'test-'+str(curSaveIndex)+'.xml'
b@2211 158
b@2211 159 class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
b@2211 160 def do_HEAD(s):
b@2211 161 s.send_response(200)
b@2211 162 s.send_header("Content-type", "text/html")
b@2211 163 s.end_headers()
b@2211 164 def do_GET(request):
b@2211 165 global pseudo_index
b@2211 166 global pseudo_files
b@2211 167 global PSEUDO_PATH
b@2211 168 if(request.client_address[0] == "127.0.0.1"):
b@2211 169 if (request.path == "/favicon.ico"):
b@2211 170 send404(request)
b@2211 171 elif (request.path.split('?',1)[0] == "/keygen.php"):
b@2211 172 keygen(request);
b@2211 173 else:
b@2211 174 if (request.path == '/'):
b@2211 175 request.path = '/index.html'
b@2211 176 elif (request.path == '/pseudo.xml'):
b@2211 177 request.path = '/'+PSEUDO_PATH + pseudo_files[pseudo_index]
b@2211 178 print request.path
b@2211 179 pseudo_index += 1
b@2211 180 pseudo_index %= len(pseudo_files)
b@2211 181 processFile(request)
b@2211 182 else:
b@2211 183 send404(request)
b@2211 184
b@2211 185 def do_POST(request):
b@2211 186 if(request.client_address[0] == "127.0.0.1"):
b@2211 187 if (request.path.rsplit('?',1)[0] == "/save" or request.path.rsplit('?',1)[0] == "/save.php"):
b@2211 188 saveFile(request)
b@2211 189 else:
b@2211 190 send404(request)
nicholas@2222 191 if sys.version_info[0] == 2:
nicholas@2222 192 def run(server_class=BaseHTTPServer.HTTPServer,handler_class=MyHandler):
nicholas@2222 193 server_address = ('', 8000)
nicholas@2222 194 httpd = server_class(server_address, handler_class)
nicholas@2222 195 httpd.serve_forever()
nicholas@2222 196 elif sys.version_info[0] == 3:
nicholas@2222 197 def run(server_class=HTTPServer,handler_class=MyHandler):
nicholas@2222 198 server_address = ('', 8000)
nicholas@2222 199 httpd = server_class(server_address, handler_class)
nicholas@2222 200 httpd.serve_forever()
b@2211 201
b@2211 202 run()