Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.