Skip to content

Instantly share code, notes, and snippets.

@nimitbhargava
Last active August 10, 2017 18:19
Show Gist options
  • Save nimitbhargava/3a75cd578b34cb5b6ad0655233879bda to your computer and use it in GitHub Desktop.
Save nimitbhargava/3a75cd578b34cb5b6ad0655233879bda to your computer and use it in GitHub Desktop.
Building A Server With HTTPBaseServer in Python | Documentation on HTTPBaseServer (https://docs.python.org/2/library/basehttpserver.html)
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
class WebServerHandler(BaseHTTPRequestHandler):
def do_GET(self):
if self.path.endswith("/hello"):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
message = ""
message += "<html><body>Hello!</body></html>"
self.wfile.write(message)
print message
return
else:
self.send_error(404, 'File Not Found: %s' % self.path)
def main():
try:
port = 8080
server = HTTPServer(('', port), WebServerHandler)
print "Web Server running on port %s" % port
server.serve_forever()
except KeyboardInterrupt:
print " ^C entered, stopping web server...."
server.socket.close()
if __name__ == '__main__':
main()
@nimitbhargava
Copy link
Author

Python Documentation on HTTPBaseServer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment