Created
August 25, 2015 19:00
-
-
Save bgulla/de05806169d79b02cc3d to your computer and use it in GitHub Desktop.
Python Webserver that serves up a flat HTML file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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: </string>%s | |
time: %s | |
socket: %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