Skip to content

Instantly share code, notes, and snippets.

@libert-xyz
Created September 22, 2016 14:18
Show Gist options
  • Save libert-xyz/7830cd9595e0a7d1968f32530a2387c1 to your computer and use it in GitHub Desktop.
Save libert-xyz/7830cd9595e0a7d1968f32530a2387c1 to your computer and use it in GitHub Desktop.
small web server using python standard library
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
class WebserverHandler(BaseHTTPRequestHandler):
def do_GET(self):
try:
if self.path.endswith('/hello'):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
output = ""
output += "<html>hello webserver</html>"
self.wfile.write(output)
print output
except IOError:
self.send_error(404, "File not found %s" %self.path)
def main():
try:
port = 8080
server = HTTPServer(('',port),WebserverHandler)
print "Webserver running on port %s" % port
server.serve_forever()
except KeyboardInterrupt:
print "^C entered, stopping web server..."
server.socket.close()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment