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