annotate pythonServer.py @ 1460:1b81ab727352

Urgent Fix. Drag End function uses clientX mouse. Fixes rogue movement after drop on OSX.
author Nicholas Jillings <nickjillings@users.noreply.github.com>
date Mon, 23 Nov 2015 15:48:07 +0000
parents c8a9825aaa05
children 1b6fa37d46a4 235594325b84
rev   line source
nickjillings@1453 1 import BaseHTTPServer
nickjillings@1453 2 from os import walk
nickjillings@1453 3 from os import path
nickjillings@1453 4 from os import listdir
nickjillings@1453 5 import inspect
nickjillings@1453 6 import os
nickjillings@1453 7 import urllib2
nickjillings@1453 8 import pickle
nickjillings@1453 9 import datetime
nickjillings@1453 10
nickjillings@1453 11 # Go to right folder.
nickjillings@1453 12 scriptdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) # script directory
nickjillings@1453 13 os.chdir(scriptdir) # does this work?
nickjillings@1453 14
nickjillings@1453 15 PSEUDO_PATH = 'example_eval/'
nickjillings@1453 16 pseudo_files = []
nickjillings@1453 17 for filename in listdir(PSEUDO_PATH):
nickjillings@1453 18 if filename.endswith('.xml'):
nickjillings@1453 19 pseudo_files.append(filename)
nickjillings@1453 20
nickjillings@1453 21 curSaveIndex = 0;
nickjillings@1453 22 curFileName = 'test-0.xml'
nickjillings@1453 23 while(path.isfile('saves/'+curFileName)):
nickjillings@1453 24 curSaveIndex += 1;
nickjillings@1453 25 curFileName = 'test-'+str(curSaveIndex)+'.xml'
nickjillings@1453 26
nickjillings@1453 27 print "Next save - " + curFileName
nickjillings@1453 28 pseudo_index = curSaveIndex % len(pseudo_files)
nickjillings@1453 29 print "Next test in pseudo-random queue - " + pseudo_files[pseudo_index]
nickjillings@1453 30
nickjillings@1453 31 def send404(s):
nickjillings@1453 32 s.send_response(404)
nickjillings@1453 33 s.send_header("Content-type", "text/html")
nickjillings@1453 34 s.end_headers()
nickjillings@1453 35
nickjillings@1453 36 def processFile(s):
nickjillings@1453 37 s.path = s.path[1:len(s.path)]
nickjillings@1453 38 st = s.path.rsplit(',')
nickjillings@1453 39 lenSt = len(st)
nickjillings@1453 40 fmt = st[lenSt-1].rsplit('.')
nickjillings@1453 41 size = path.getsize(urllib2.unquote(s.path))
nickjillings@1453 42 fileDump = open(urllib2.unquote(s.path))
nickjillings@1453 43 s.send_response(200)
nickjillings@1453 44
nickjillings@1453 45 if (fmt[1] == 'html'):
nickjillings@1453 46 s.send_header("Content-type", 'text/html')
nickjillings@1453 47 elif (fmt[1] == 'css'):
nickjillings@1453 48 s.send_header("Content-type", 'text/css')
nickjillings@1453 49 elif (fmt[1] == 'js'):
nickjillings@1453 50 s.send_header("Content-type", 'application/javascript')
nickjillings@1453 51 else:
nickjillings@1453 52 s.send_header("Content-type", 'application/octet-stream')
nickjillings@1453 53 s.send_header("Content-Length", size)
nickjillings@1453 54 s.end_headers()
nickjillings@1453 55 s.wfile.write(fileDump.read())
nickjillings@1453 56 fileDump.close()
nickjillings@1453 57
nickjillings@1453 58 def saveFile(self):
nickjillings@1453 59 global curFileName
nickjillings@1453 60 global curSaveIndex
nickjillings@1453 61 varLen = int(self.headers['Content-Length'])
nickjillings@1453 62 postVars = self.rfile.read(varLen)
nickjillings@1453 63 print curFileName
nickjillings@1453 64 file = open('saves/'+curFileName,'w')
nickjillings@1453 65 curSaveIndex += 1;
nickjillings@1453 66 curFileName = 'test-'+str(curSaveIndex)+'.xml'
nickjillings@1453 67 print curFileName
nickjillings@1453 68 file.write(postVars)
nickjillings@1453 69 file.close()
nickjillings@1453 70 self.send_response(200)
nickjillings@1453 71 self.send_header("Content-type", "text/xml")
nickjillings@1453 72 self.end_headers()
nickjillings@1457 73 self.wfile.write('<response state="OK"><message>OK</message><file>"saves/'+curFileName+'"</file></response>')
nickjillings@1453 74
nickjillings@1453 75 class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
nickjillings@1453 76 def do_HEAD(s):
nickjillings@1453 77 s.send_response(200)
nickjillings@1453 78 s.send_header("Content-type", "text/html")
nickjillings@1453 79 s.end_headers()
nickjillings@1453 80 def do_GET(request):
nickjillings@1453 81 global pseudo_index
nickjillings@1453 82 global pseudo_files
nickjillings@1453 83 global PSEUDO_PATH
nickjillings@1453 84 if(request.client_address[0] == "127.0.0.1"):
nickjillings@1453 85 if (request.path == "/favicon.ico"):
nickjillings@1453 86 send404(request)
nickjillings@1453 87 else:
nickjillings@1453 88 if (request.path == '/'):
nickjillings@1453 89 request.path = '/index.html'
nickjillings@1453 90 elif (request.path == '/pseudo.xml'):
nickjillings@1453 91 request.path = '/'+PSEUDO_PATH + pseudo_files[pseudo_index]
nickjillings@1453 92 print request.path
nickjillings@1453 93 pseudo_index += 1
nickjillings@1453 94 pseudo_index %= len(pseudo_files)
nickjillings@1453 95 processFile(request)
nickjillings@1453 96 else:
nickjillings@1453 97 send404(request)
nickjillings@1453 98
nickjillings@1453 99 def do_POST(request):
nickjillings@1453 100 if(request.client_address[0] == "127.0.0.1"):
nickjillings@1453 101 if (request.path == "/save"):
nickjillings@1453 102 saveFile(request)
nickjillings@1453 103 else:
nickjillings@1453 104 send404(request)
nickjillings@1453 105
nickjillings@1453 106 def run(server_class=BaseHTTPServer.HTTPServer,
nickjillings@1453 107 handler_class=MyHandler):
nickjillings@1453 108 server_address = ('', 8000)
nickjillings@1453 109 httpd = server_class(server_address, handler_class)
nickjillings@1453 110 httpd.serve_forever()
nickjillings@1453 111
nickjillings@1453 112 run()