annotate python2.py @ 2017:4a14c8a65af2

Merge from 156:402bb0f56dc4
author Nicholas Jillings <nickjillings@users.noreply.github.com>
date Mon, 01 Jun 2015 12:56:15 +0100
parents 97f23a756fe5
children
rev   line source
nickjillings@1626 1 import BaseHTTPServer
nickjillings@1626 2 from os import walk
nickjillings@1626 3 from os import path
nickjillings@1626 4 import urllib2
nickjillings@1627 5 import pickle
nickjillings@1627 6 import datetime
nickjillings@1627 7
nickjillings@1627 8 curSaveIndex = 0;
nickjillings@1627 9 curFileName = 'test-0.xml'
nickjillings@1627 10 while(path.isfile('saves/'+curFileName)):
nickjillings@1627 11 curSaveIndex += 1;
nickjillings@1627 12 curFileName = 'test-'+str(curSaveIndex)+'.xml'
nickjillings@1627 13
nickjillings@1627 14 print curFileName
nickjillings@1626 15
nickjillings@1626 16 def send404(s):
nickjillings@1626 17 s.send_response(404)
nickjillings@1626 18 s.send_header("Content-type", "text/html")
nickjillings@1626 19 s.end_headers()
nickjillings@1626 20
nickjillings@1626 21 def processFile(s):
nickjillings@1626 22 s.path = s.path[1:len(s.path)]
nickjillings@1626 23 st = s.path.rsplit(',')
nickjillings@1626 24 lenSt = len(st)
nickjillings@1626 25 fmt = st[lenSt-1].rsplit('.')
nickjillings@1626 26 size = path.getsize(urllib2.unquote(s.path))
nickjillings@1626 27 fileDump = open(urllib2.unquote(s.path))
nickjillings@1626 28 s.send_response(200)
nickjillings@1626 29
nickjillings@1626 30 if (fmt[1] == 'html'):
nickjillings@1626 31 s.send_header("Content-type", 'text/html')
nickjillings@1626 32 elif (fmt[1] == 'css'):
nickjillings@1626 33 s.send_header("Content-type", 'text/css')
nickjillings@1626 34 elif (fmt[1] == 'js'):
nickjillings@1626 35 s.send_header("Content-type", 'application/javascript')
nickjillings@1626 36 else:
nickjillings@1626 37 s.send_header("Content-type", 'application/octet-stream')
nickjillings@1626 38 s.send_header("Content-Length", size)
nickjillings@1626 39 s.end_headers()
nickjillings@1626 40 s.wfile.write(fileDump.read())
nickjillings@1626 41 fileDump.close()
nickjillings@1626 42
nickjillings@1627 43 def saveFile(self):
nickjillings@1627 44 global curFileName
nickjillings@1627 45 global curSaveIndex
nickjillings@1627 46 varLen = int(self.headers['Content-Length'])
nickjillings@1627 47 postVars = self.rfile.read(varLen)
nickjillings@1627 48 print curFileName
nickjillings@1627 49 file = open('saves/'+curFileName,'w')
nickjillings@1627 50 curSaveIndex += 1;
nickjillings@1627 51 curFileName = 'test-'+str(curSaveIndex)+'.xml'
nickjillings@1627 52 print curFileName
nickjillings@1627 53 file.write(postVars)
nickjillings@1627 54 file.close()
nickjillings@1627 55 self.send_response(200)
nickjillings@1627 56 self.send_header("Content-type", "text/xml")
nickjillings@1627 57 self.end_headers()
nickjillings@1627 58 self.wfile.write('<response><state>OK</state><file>saves/'+curFileName+'</file></response>')
nickjillings@1626 59
nickjillings@1626 60 class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
nickjillings@1626 61 def do_HEAD(s):
nickjillings@1626 62 s.send_response(200)
nickjillings@1626 63 s.send_header("Content-type", "text/html")
nickjillings@1626 64 s.end_headers()
nickjillings@1626 65 def do_GET(request):
nickjillings@1626 66 if(request.client_address[0] == "127.0.0.1"):
nickjillings@1626 67 if (request.path == "/favicon.ico"):
nickjillings@1626 68 send404(request)
nickjillings@1626 69 else:
nickjillings@1626 70 if (request.path == '/'):
nickjillings@1626 71 request.path = '/index.html'
nickjillings@1626 72 processFile(request)
nickjillings@1626 73 else:
nickjillings@1626 74 send404(request)
nickjillings@1627 75 def do_POST(request):
nickjillings@1627 76 if(request.client_address[0] == "127.0.0.1"):
nickjillings@1627 77 if (request.path == "/save"):
nickjillings@1627 78 saveFile(request)
nickjillings@1627 79 else:
nickjillings@1627 80 send404(request)
nickjillings@1626 81
nickjillings@1626 82 def run(server_class=BaseHTTPServer.HTTPServer,
nickjillings@1626 83 handler_class=MyHandler):
nickjillings@1626 84 server_address = ('', 8000)
nickjillings@1626 85 httpd = server_class(server_address, handler_class)
nickjillings@1626 86 httpd.serve_forever()
nickjillings@1626 87
nickjillings@1626 88 run()