Hari's Corner

Humour, comics, tech, law, software, reviews, essays, articles and HOWTOs intermingled with random philosophy now and then

CGI scripts have always mystified me

Filed under: Software and Technology by Hari
Posted on Sat, Mar 22, 2008 at 17:39 IST (last updated: Wed, Jul 16, 2008 @ 20:38 IST)

Having used PHP as the logical choice for web development, I've always been a little mystified by cgi-bin scripts and never bothered to learn how they work. But no longer.

CGI scripts are nothing but scripts or executable programs. Apache simply executes them and renders the results. Of course, because every CGI program is an external application and requires the web server to start a new process, it makes the whole thing very processor intensive. The modern method of embedding scripts in web pages (most popularly PHP/ASP) which can be directly interpreted by Apache is a far more efficient method and I suppose more logical from a web designer's point of view.

However, there are still a few advantages to CGI scripts: you can use whatever language you wish to (plain shell scripting, Perl, Python and even compiled C executables). This offers many more options to the developer. Also in many cases CGI scripts are not limited to server "modules" and can directly use the underlying Operating System's functionality.

Rather than reinvent the wheel however, I'll point you to Apache's CGI tutorial which is a must read for any webmaster, web developer or server administrator.

Yes, CGI can be intimidating and complex CGI scripts can be a headache to maintain, but it's nevertheless useful to have an idea about how they work since so many websites still utilize this method of delivering dynamic content.

14 comment(s)

  1. Yeah, CGI can be handy but if I needed it, I'd probably prefer to keep such scripts on an internal network. There's just too many alternatives to use for public websites that are more secure, etc.

    I'm currently learning python and might start diving into some mod_python for apache as well, really liking it over learning/getting better with Perl.

    Comment by (visitor) on Sat, Mar 22, 2008 @ 18:29 IST #
  2. I tried mod_python but couldn't understand head or tail of it. :(

    In general, Python's documentation sucks. That is the biggest impediment to using that language instead of, say PHP or even Perl.

    Comment by hari (blog owner) on Sat, Mar 22, 2008 @ 18:51 IST #
  3. Oh man, go read this then: http://diveintopython.org

    It was recommended by my boss as the better of the books to learn Python. You can read the entire thing online or download it. I also purchased it cause I'm weird like that and like hard copies of things when I'm away from a computer.

    Comment by (visitor) on Sun, Mar 23, 2008 @ 02:32 IST #
  4. Drew, I'll take a look at that book. Python by itself is a simple enough language. What I am having trouble is using Python for web development.

    Comment by hari (blog owner) on Sun, Mar 23, 2008 @ 07:48 IST #
  5. Look into http://www.djangoproject.com/ to help with web development. All the python programmers I know swear by it.

    Comment by drew (visitor) on Sun, Mar 23, 2008 @ 18:42 IST #
  6. Actually, Drew, I find myself prefer plain Vanilla CGI scripting over any fancy mod-* for Python.

    I feel it's more natural to use Python just as a plain programming language as it's intended to be and then use the modules required for web programming rather than learn another "framework" which has its own set of implementation rules.

    Comment by hari (blog owner) on Sun, Mar 23, 2008 @ 20:34 IST #
  7. Apart from http://diveintopython.org, try http://openbookproject.net//thinkCSpy/ and http://www.mindview.net/Books/TIPython

    Comment by od (visitor) on Sun, Mar 23, 2008 @ 21:28 IST #
  8. Thanks, od. Yes, Python is a great general-purpose language.

    I've already written a Python script to generate a website using text files.

    Comment by hari (blog owner) on Mon, Mar 24, 2008 @ 07:42 IST #
  9. I'm with you on this, Hari. I've done Perl in the past, but never dived into anything else other than PHP. Of course, the last web application I wrote was way back when Perl was preferred over PHP.

    Comment by RT Cunningham (visitor) on Mon, Mar 24, 2008 @ 11:45 IST #
  10. RT, I'm currently writing a Python script which will help me maintain my harishankar.org reviews site. It will generate static HTML files for me to upload rather than work as a CGI script though (I wanted static HTML because it produces cleaner URLs)

    While I believe Python and Perl have a lot of advantages over PHP going a bit deeper into them, PHP allows a lot of "lazy coding" too and is specifically targetted at "web designers" rather than "programmers". The biggest draw of PHP is its ability to be used as an embedded language so that it becomes very convenient to work around non-interpreted code (most commonly HTML or XML). Python and Perl for web development are not so intuitive but I think once you learn to use them, they can offer much more power and more maintainable code.

    Python and Perl are much more traditional programming languages and web development is just one aspect of them.

    Comment by hari (blog owner) on Mon, Mar 24, 2008 @ 12:43 IST #
  11. I'm not Perl master and teaching myself always came in spurts, never really getting that good with it, I'm finding myself enjoying python much more.

    I don't want to be some master programmer, but knowing a little bit of these help my career as a systems administrator.

    Comment by (visitor) on Mon, Mar 24, 2008 @ 21:29 IST #
  12. Drew, I find that writing "small" scripts is a really useful way to develop your skills and knowledge. Also when you get familiar with the language, you tend to put it to more complex usage.

    Comment by hari (blog owner) on Mon, Mar 24, 2008 @ 21:39 IST #
  13. Thanks for the useful post, I've never really understood CGI scripts myself either until now. But I think I'll wait for myself to properly understand PHP before diving into CGI, if ever.

    Comment by Joshua Goodwin (visitor) on Wed, Apr 2, 2008 @ 00:14 IST #
  14. Joshua, it's not necessary to "know" PHP before you know CGI.

    Think of it this way: PHP, ASP, JSP... etc. are languages that are embedded in documents (mostly HTML) and interpreted by the web server software directly (or through extensions like mod_php for Apache etc.)

    CGI scripts are real programs (or scripts) that are called by the server software to run as a separate process and their output is returned to the server which then serves that as a web page.

    Comment by hari (blog owner) on Wed, Apr 2, 2008 @ 08:29 IST #

Comments closed

The blog owner has closed further commenting on this entry.