Commit 1bcf4c991fd2d763d2041131193d642ef1771790

Authored by Andrew Buss
1 parent 79d135c5be

Added local server

Showing 1 changed file with 34 additions and 1 deletions Side-by-side Diff

local_server.py View file @ 1bcf4c9
1   -__author__ = 'andrew'
  1 +from SimpleHTTPServer import SimpleHTTPRequestHandler
  2 +import SocketServer
  3 +import os
  4 +
  5 +
  6 +class SPARequestHandler(SimpleHTTPRequestHandler):
  7 + def send_head(self):
  8 + self.path = self.path.lstrip('/app')
  9 + path = self.translate_path(self.path)
  10 + f = None
  11 + if os.path.isdir(path):
  12 + for index in "index.html", "index.htm":
  13 + index = os.path.join(path, index)
  14 + if os.path.exists(index):
  15 + path = index
  16 + break
  17 + else:
  18 + return self.list_directory(path)
  19 + ctype = self.guess_type(path)
  20 + if ctype.startswith('text/'):
  21 + mode = 'r'
  22 + else:
  23 + mode = 'rb'
  24 + try:
  25 + f = open(path, mode)
  26 + except IOError:
  27 + return open('home.html', 'r')
  28 + self.send_response(200)
  29 + self.send_header("Content-type", ctype)
  30 + self.end_headers()
  31 + return f
  32 +
  33 +httpd = SocketServer.TCPServer(("", 80), SPARequestHandler)
  34 +httpd.serve_forever()