"""
PythonHTTPServer.py - simple Web Server using BaseHTTPServer module in Python
"""

from BaseHTTPServer import HTTPServer
from BaseHTTPServer import BaseHTTPRequestHandler
import string

"""
class ourRequestHandler - deals with requests of the Web Server
"""
class ourRequestHandler(BaseHTTPRequestHandler):
  """
  do_GET helps with GET requests
  """
  def do_GET(self):
    # always return 200 = OK 
    self.ourPrintCustomHTTPResponse(200)
    
    # HTML output
    self.wfile.write("<!DOCTYPE html><html><head>")
    self.wfile.write("<title>PythonHTTPServer.py - simple Web Server using BaseHTTPServer module in Python - GET method</title>")
    self.wfile.write("</head><body style='background-color: yellow;'><p>Welcome to our simple local Python Web Server.</p>")
    self.wfile.write("<p style='color: green;'>Method GET string: " + self.path + "</p><p style='color: purple;'>")
    
    # ... browser headers ...
    self.ourPrintBrowserHeaders()
    
    # ... and finish up
    self.wfile.write("</p></body></html>")
    
  """
  do_POST helps with POST requests
  """
  def do_POST(self):
    # always return 200 = OK 
    self.ourPrintCustomHTTPResponse(200)
    
    # HTML output
    self.wfile.write("<!DOCTYPE html><html><head>")
    self.wfile.write("<title>PythonHTTPServer.py - simple Web Server using BaseHTTPServer module in Python - POST method</title>")
    self.wfile.write("</head><body style='background-color: pink;'><p style='color: red;'>")
    self.ourPrintBrowserHeaders()
    self.wfile.write("<b>Method Post Data:</b></p><br><p style='color: blue;'>")
    
    # ... post length logic ...
    if self.headers.dict.has_key("content-length"):
      # ... string to int ...
      our_content_length = string.atoi(self.headers.dict["content-length"])
      
      # ... read from client ...
      our_raw_post_data = self.rfile.read(our_content_length)
      self.wfile.write(our_raw_post_data)
      
    # ... and finish up
    self.wfile.write("</p></body></html>")

  """
  ourPrintBrowserHeaders prints HTTP headers
  """
  def ourPrintBrowserHeaders(self):
    # navigate dictionary header name/value pairs
    self.wfile.write("<b>Headers: <br>")
    our_header_keys = self.headers.dict.keys()
    for ourkey in our_header_keys:
      self.wfile.write("<b>" + ourkey + "</b>: ")
      self.wfile.write(self.headers.dict[ourkey] + "<br>")


  """
  ourPrintCustomHTTPResponse takes response and sends code and custom headers back to browser
  """
  def ourPrintCustomHTTPResponse(self, ourrespcode):
    # send back HTTP code
    self.send_response(ourrespcode)
    
    # ... inform ...
    self.send_header("Content-type", "text/html")
    
    # ... describe software ...
    self.send_header("Server", "ourRequestHandler :-)")
    
    # ... close headers
    self.end_headers()
    

# start server on port 2112 so a URL goes http://localhost:2112/pathtofile
ourServer = HTTPServer(('', 2112), ourRequestHandler)

# ... loop for 5 requests ...
for ourlp in range(5):
  ourServer.handle_request()
