Tuesday, 24 April 2012

CGI, where've you bin all my life?

I'd been meaning to play around with CGI (Common Gateway Interface) programming for some time as a way of making simple functional websites. I finally got round to it last month, thanks to a great introductory page at Tutorials Point. What I did not realise is quite how easy it was.

I've still only really scratched the surface and scanned over the page to get something up quickly but, in essence, you only need three things:
1. A webserver that supports CGI.

2. An html page containing some "form" code that contains a submit button and (optionally) some input options (e.g. text boxes or checkboxes).

3. A python script (or another language) that generates HTML code based on the variables and values from the form.
And that's essentially it. Actually, you don't even need (2), as you can feed variables directly to the cgi script, but it makes it easier for the user, I think.

My first attempt at this can be found here. It's a bit of silly fun but it shows what can be done with just a few simple lines of code. I've cheated a little bit by using an existing python module to generate the middle of the HTML code but, in a way, that's the point - you can easily adapt existing functional code to output text.

In this case, I use the random "Zen wisdom" text strings that are generated in my code to lighten up error messages when I'm debugging. (If you ever use one of my programs, you sometimes come across such an error message, which always causes confusion (and usually embarrassment for me!) but I think it's a small price to pay for making debugging more fun!) The scary thing is how often the random Zen Wisdoms sound deep and meaningful, e.g.
"It is bold to play jenga with blocks of passion."
Well, maybe not that deep and meaningful!


  1. This comment has been removed by a blog administrator.

  2. It is wise to attend a conference about Chaos because an exhausted ladyboy ponders....

  3. Like I said, not that deep and meaningful! (It appeared twice, so I deleted the first one, hope you don't mind.)

    Apparently, CGI is not the way forward and I need to check out CherryPy. Watch this space...


Thanks for leaving a comment! (Unless you're a spammer, in which case please stop - I am only going to delete it. You are just wasting your time and mine.)