Saturday, April 18, 2009

Bernardo The Smallest HTTPD Ever!

Sometimes I need an HTTP Server that allows me to run C or Python code or other scripts, and I need something that require 0 seconds of configuration. So I've decided to write a small httpd that does this work. (Use only for your test, not on a work machine!).

Bernardo (The name is inspired by the Zorro's servant) is a small HTTPD (Less that 200 lines of code) that allows you to Execute C, Python, Perl, PHP code or just "display" a file.

The Usage is Very simple: bernardo-httpd where www dir will be di root of your server and port is the TCP Port of the Service.

And now a simple code Example. You need to complete the HTTP Response Headers and then you can output your data. http://localhost:8080/ will display the "Hello Python" page.

#!/usr/bin/env python

import os

if __name__ == '__main__':
print "Content-Type: text/html"

print "<html>"
print "<head><title>Hello Python</title></head>"
print "<body>"
print "<h1>Hello Python</h1>"
print "<p><b>Request Method</b>: ", os.environ['REQUEST_METHOD']
print "<p><b>Query String</b>: ", os.environ['QUERY_STRING']
print "</body>"
print "<html>"

The Source Code is Available Here: Bernardo HTTPD Source Code.

1 comment:

  1. #!/usr/local/bin/perl

    # -- my first perl script!

    print "Content-type: text/html\n\n";

    print <<"EOF";

    Hello, world!

    Hello, world!