Saturday, January 26, 2008

Smallest ever web server in Python

Suppose you want to compare Java to Python as far as writing a simple web server is concerned :-).

Without much ado, here's the code that does the same, in about half as many lines of code:

import os
import BaseHTTPServer


class SimplestWebHandler(
BaseHTTPServer.BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.end_headers()
self.wfile.write('Hello, you requested '
+ self.path)


def main():
server = BaseHTTPServer.HTTPServer(('', 8000),
SimplestWebHandler)
print 'Server is looping.'
server.serve_forever()


if __name__ == '__main__':
main()

3 comments:

  1. you can write even a smaller one using xinetd, even with xinetd code :)

    ReplyDelete
  2. If that's considered "writing a web server", so does this:

    /etc/init.d/apache start

    Note that mine is much more feature rich :)

    ReplyDelete
  3. Is this the Google App Engine?

    ReplyDelete