Skip to content

Instantly share code, notes, and snippets.

@bgulla
Created August 25, 2015 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bgulla/de05806169d79b02cc3d to your computer and use it in GitHub Desktop.
Save bgulla/de05806169d79b02cc3d to your computer and use it in GitHub Desktop.
Python Webserver that serves up a flat HTML file.
#!/usr/bin/python
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
from time import gmtime, strftime
import sys,random,socket
PORT_NUMBER = 8000
HOSTNAME = socket.gethostname()
if len(sys.argv) > 1:
print "FOUND PORT ASSIGNMENT:", sys.argv[1]
PORT_NUMBER = int(sys.argv[1])
time= strftime("%Y-%m-%d %H:%M:%S", gmtime())
#This class will handles any incoming request from
#the browser
class myHandler(BaseHTTPRequestHandler):
#Handler for the GET requests
def do_GET(self):
self.send_response(200)
self.send_header('Content-type','text/html')
self.end_headers()
html = """
<html>
<body style='background-color:black; color: cyan;'>
<p><br><br>
<pre>
<strong>host:&nbsp;</string>%s
time:&nbsp; %s
socket:&nbsp; %s
</pre>
</p>
</body>
</html>
""" % ( socket.gethostname() , time, PORT_NUMBER)
self.wfile.write(html)
try:
#Create a web server and define the handler to manage the
#incoming request
server = HTTPServer(('', PORT_NUMBER), myHandler)
print 'Started httpserver on port ' , PORT_NUMBER
#Wait forever for incoming htto requests
server.serve_forever()
except KeyboardInterrupt:
print '^C received, shutting down the web server'
server.socket.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment