annotate pythonServer.py @ 750:c9c010690d56

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