annotate scripts/pythonServer.py @ 2230:4e334c942d41

Fixed pythonServer.py for python v 3.x
author Nicholas Jillings <nicholas.jillings@mail.bcu.ac.uk>
date Thu, 14 Apr 2016 20:44:35 +0100
parents 4d1aa94202e3
children 6ffef0759d29
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@2230 29 PSEUDO_PATH = '../tests/'
b@2211 30 pseudo_files = []
b@2211 31 for filename in listdir(PSEUDO_PATH):
nicholas@2230 32 print(filename)
b@2211 33 if filename.endswith('.xml'):
b@2211 34 pseudo_files.append(filename)
b@2211 35
b@2211 36 curSaveIndex = 0;
b@2211 37 curFileName = 'test-0.xml'
nicholas@2222 38 while(path.isfile('../saves/'+curFileName)):
nicholas@2222 39 curSaveIndex += 1;
nicholas@2222 40 curFileName = 'test-'+str(curSaveIndex)+'.xml'
b@2211 41
nicholas@2230 42 if len(pseudo_files) > 0:
nicholas@2230 43 pseudo_index = curSaveIndex % len(pseudo_files)
nicholas@2230 44 else:
nicholas@2230 45 pseudo_index = 0
b@2211 46
nicholas@2230 47 print('URL: http://localhost:8000/index.html')
b@2211 48
b@2211 49 def send404(s):
nicholas@2222 50 s.send_response(404)
nicholas@2222 51 s.send_header("Content-type", "text/html")
nicholas@2222 52 s.end_headers()
b@2211 53
b@2211 54 def processFile(s):
nicholas@2222 55 if sys.version_info[0] == 2:
nicholas@2222 56 s.path = s.path.rsplit('?')
nicholas@2222 57 s.path = s.path[0]
nicholas@2222 58 s.path = s.path[1:len(s.path)]
nicholas@2222 59 st = s.path.rsplit(',')
nicholas@2222 60 lenSt = len(st)
nicholas@2222 61 fmt = st[lenSt-1].rsplit('.')
nicholas@2222 62 fpath = "../"+urllib2.unquote(s.path)
nicholas@2222 63 size = path.getsize(fpath)
nicholas@2222 64 fileDump = open(fpath)
nicholas@2222 65 s.send_response(200)
nicholas@2222 66
nicholas@2222 67 if (fmt[1] == 'html'):
nicholas@2222 68 s.send_header("Content-type", 'text/html')
nicholas@2222 69 elif (fmt[1] == 'css'):
nicholas@2222 70 s.send_header("Content-type", 'text/css')
nicholas@2222 71 elif (fmt[1] == 'js'):
nicholas@2222 72 s.send_header("Content-type", 'application/javascript')
nicholas@2222 73 else:
nicholas@2222 74 s.send_header("Content-type", 'application/octet-stream')
nicholas@2222 75 s.send_header("Content-Length", size)
nicholas@2222 76 s.end_headers()
nicholas@2222 77 s.wfile.write(fileDump.read())
nicholas@2222 78 fileDump.close()
nicholas@2222 79 elif sys.version_info[0] == 3:
nicholas@2222 80 s.path = s.path.rsplit('?')
nicholas@2222 81 s.path = s.path[0]
nicholas@2222 82 s.path = s.path[1:len(s.path)]
nicholas@2222 83 st = s.path.rsplit(',')
nicholas@2222 84 lenSt = len(st)
nicholas@2222 85 fmt = st[lenSt-1].rsplit('.')
nicholas@2230 86 fpath = "../"+urllib2.parse.unquote(s.path)
nicholas@2222 87 s.send_response(200)
nicholas@2222 88 if (fmt[1] == 'html'):
nicholas@2222 89 s.send_header("Content-type", 'text/html')
nicholas@2222 90 fileDump = open(fpath, encoding='utf-8')
nicholas@2222 91 fileBytes = bytes(fileDump.read(), "utf-8")
nicholas@2222 92 fileDump.close()
nicholas@2222 93 elif (fmt[1] == 'css'):
nicholas@2222 94 s.send_header("Content-type", 'text/css')
nicholas@2222 95 fileDump = open(fpath, encoding='utf-8')
nicholas@2222 96 fileBytes = bytes(fileDump.read(), "utf-8")
nicholas@2222 97 fileDump.close()
nicholas@2222 98 elif (fmt[1] == 'js'):
nicholas@2222 99 s.send_header("Content-type", 'application/javascript')
nicholas@2222 100 fileDump = open(fpath, encoding='utf-8')
nicholas@2222 101 fileBytes = bytes(fileDump.read(), "utf-8")
nicholas@2222 102 fileDump.close()
nicholas@2222 103 else:
nicholas@2222 104 s.send_header("Content-type", 'application/octet-stream')
nicholas@2222 105 fileDump = open(fpath, 'rb')
nicholas@2222 106 fileBytes = fileDump.read()
nicholas@2222 107 fileDump.close()
nicholas@2222 108 s.send_header("Content-Length", len(fileBytes))
nicholas@2222 109 s.end_headers()
nicholas@2222 110 s.wfile.write(fileBytes)
b@2211 111
b@2211 112 def keygen(s):
b@2211 113 reply = ""
b@2211 114 options = s.path.rsplit('?')
b@2211 115 options = options[1].rsplit('=')
b@2211 116 key = options[1]
nicholas@2230 117 print("Registered key "+key)
b@2211 118 if os.path.isfile("saves/save-"+key+".xml"):
b@2211 119 reply = "<response><state>NO</state><key>"+key+"</key></response>"
b@2211 120 else:
b@2211 121 reply = "<response><state>OK</state><key>"+key+"</key></response>"
b@2211 122 s.send_response(200)
b@2211 123 s.send_header("Content-type", "application/xml")
b@2211 124 s.end_headers()
b@2211 125 s.wfile.write(reply)
nicholas@2222 126 file = open("../saves/save-"+key+".xml",'w')
b@2211 127 file.write("<waetresult key="+key+"/>")
b@2211 128 file.close();
b@2211 129
b@2211 130 def saveFile(self):
nicholas@2222 131 global curFileName
nicholas@2222 132 global curSaveIndex
nicholas@2222 133 options = self.path.rsplit('?')
nicholas@2222 134 options = options[1].rsplit('=')
nicholas@2222 135 key = options[1]
nicholas@2222 136 varLen = int(self.headers['Content-Length'])
nicholas@2222 137 postVars = self.rfile.read(varLen)
nicholas@2230 138 print("Saving file key "+key)
nicholas@2222 139 file = open('../saves/save-'+key+'.xml','w')
nicholas@2222 140 file.write(postVars)
nicholas@2222 141 file.close()
nicholas@2222 142 try:
nicholas@2222 143 wbytes = os.path.getsize('../saves/save-'+key+'.xml')
nicholas@2222 144 except OSError:
nicholas@2222 145 self.send_response(200)
nicholas@2222 146 self.send_header("Content-type", "text/xml")
nicholas@2222 147 self.end_headers()
nicholas@2222 148 self.wfile.write('<response state="error"><message>Could not open file</message></response>')
nicholas@2222 149 self.send_response(200)
nicholas@2222 150 self.send_header("Content-type", "text/xml")
nicholas@2222 151 self.end_headers()
nicholas@2222 152 self.wfile.write('<response state="OK"><message>OK</message><file bytes="'+str(wbytes)+'">"saves/'+curFileName+'"</file></response>')
nicholas@2222 153 curSaveIndex += 1
nicholas@2222 154 curFileName = 'test-'+str(curSaveIndex)+'.xml'
b@2211 155
nicholas@2230 156 def http_do_HEAD(s):
nicholas@2230 157 s.send_response(200)
nicholas@2230 158 s.send_header("Content-type", "text/html")
nicholas@2230 159 s.end_headers()
b@2211 160
nicholas@2230 161 def http_do_GET(request):
nicholas@2230 162 if(request.client_address[0] == "127.0.0.1"):
nicholas@2230 163 if (request.path == "/favicon.ico"):
nicholas@2230 164 send404(request)
nicholas@2230 165 elif (request.path.split('?',1)[0] == "/keygen.php"):
nicholas@2230 166 keygen(request);
nicholas@2230 167 else:
nicholas@2230 168 request.path = request.path.split('?',1)[0]
nicholas@2230 169 if (request.path == '/'):
nicholas@2230 170 request.path = '/index.html'
nicholas@2230 171 elif (request.path == '/pseudo.xml'):
nicholas@2230 172 request.path = '/'+PSEUDO_PATH + pseudo_files[pseudo_index]
nicholas@2230 173 print(request.path)
nicholas@2230 174 pseudo_index += 1
nicholas@2230 175 pseudo_index %= len(pseudo_files)
nicholas@2230 176 processFile(request)
nicholas@2230 177 else:
nicholas@2230 178 send404(request)
nicholas@2230 179
nicholas@2230 180 def http_do_POST(request):
nicholas@2230 181 if(request.client_address[0] == "127.0.0.1"):
nicholas@2230 182 if (request.path.rsplit('?',1)[0] == "/save" or request.path.rsplit('?',1)[0] == "/save.php"):
nicholas@2230 183 saveFile(request)
nicholas@2230 184 else:
nicholas@2230 185 send404(request)
nicholas@2230 186
nicholas@2222 187 if sys.version_info[0] == 2:
nicholas@2230 188 class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
nicholas@2230 189 def do_HEAD(s):
nicholas@2230 190 http_do_HEAD(s)
nicholas@2230 191 def do_GET(request):
nicholas@2230 192 http_do_GET(request)
nicholas@2230 193 def do_POST(request):
nicholas@2230 194 http_do_POST(request)
nicholas@2222 195 def run(server_class=BaseHTTPServer.HTTPServer,handler_class=MyHandler):
nicholas@2222 196 server_address = ('', 8000)
nicholas@2222 197 httpd = server_class(server_address, handler_class)
nicholas@2222 198 httpd.serve_forever()
nicholas@2230 199 run()
nicholas@2222 200 elif sys.version_info[0] == 3:
nicholas@2230 201 class MyHandler(BaseHTTPRequestHandler):
nicholas@2230 202 def do_HEAD(s):
nicholas@2230 203 send404(s)
nicholas@2230 204 def do_GET(request):
nicholas@2230 205 http_do_GET(request)
nicholas@2230 206 def do_POST(request):
nicholas@2230 207 http_do_POST(request)
nicholas@2222 208 def run(server_class=HTTPServer,handler_class=MyHandler):
nicholas@2222 209 server_address = ('', 8000)
nicholas@2222 210 httpd = server_class(server_address, handler_class)
nicholas@2222 211 httpd.serve_forever()
nicholas@2230 212 run()