annotate pythonServer-3.py @ 1365:c4156df1dcb5

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