Bernardo The Smallest HTTPD Ever!

April 18, 2009 Matteo Bertozzi | Filed Under Utils | 1 Comment

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/test.py will display the “Hello Python” page.

#!/usr/bin/env python

import os

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

    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 »

RSS feed for comments on this post. TrackBack URI

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

    # hello.pl — my first perl script!

    print “Content-type: text/html\n\n”;

    print <<”EOF”;

    Hello, world!

    Hello, world!

    EOF

    Comment by even smaller — May 12, 2009 #

Leave a comment

XHTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>