annotate pythonServer-3.py @ 1303:ade3acb0cee3

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