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