annotate pythonServer-3.py @ 2122:cac0706b9a41

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