Tuesday, January 20, 2015

tiny "Hello World" webserver

I'm writing a Nginx configuration that will wrap password protection on top of a bunch of dependent web servers. For testing, I want a few "hello world" type web servers, each of one gives a different message, but is really tiny.

The following is what I came up with -- great for testing.

# USAGE:
# python ./hello.py free beer 

import SimpleHTTPServer
import StringIO
import SocketServer
import sys

PORT = 8000

class MyHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
    MESSAGE = ' '.join(sys.argv[1:])
    
    def send_head(self):
        self.send_response(200)
        self.send_header("Content-type", 'text/plain')
        self.end_headers()
        return StringIO.StringIO(self.MESSAGE)
    
httpd = SocketServer.TCPServer(("", PORT), MyHandler)

print "serving at port", PORT
httpd.serve_forever()

No comments:

Post a Comment