3.2.2 Hello World!

The best way to demonstrate the basics is to create a "hello world!" program. This tutorial uses Python and we will assume you are using the Apache Web Server. In a text editor type the following into a file called hello.cgi:

#!/usr/bin/env python

print "Content-type: text/html\n\n"
print "<html>Hello world!</html>"

The first line tells the web server where the Python program is on the server. It is usually in /usr/bin/python or /usr/local/bin/python so you must change the first line to suit your server but you must remember the #! at the start, the slashs are forward slashes, and that you must have a least one completely blank line before you start anything else.

Warning: The other thing which sometimes casues problems is line endings. UNIX uses LF as a line feed, windows doesn't. To confuse matters further some FTP clients try to automatically convert the line endings for you. My tip is to use a text editor such as SciTE which supports both types of line endings. Set Options->Line Ending Characters->LF and then Options->Convert Line Ending Characters to make sure you are using LF line feeds. Then you can upload your file in Binary mode on the FTP client and you can guarantee your line endings are correct

Back to our Hello World! program. The line containing text/html is an HTTP header which tells the web browser to expect HTML to be sent to it. If your CGI script sent a GIF image, for example, you would use image/gif.

The last line prints the HTML code to the web browser.

Once you have written your script you will then need to upload it to a directory which supports execution of scripts using FTP software. This directory will normally be called cgi-bin or cgi. Next you will need to change the file permissions of your script using the UNIX command chmod 755 hello.cgi or using your FTP software to change the file permissions so that everyone can read and execute your file but only you, the owner, can write it. Now, using your web browser visit the hello.cgi script by typing http://www.yourservername.com/cgi-bin/hello.cgi or something equivalent for where your file is on the internet.

If all goes well you should see the text:

Hello world

If not, check you have followed the instructions correctly. If you get an error message saying something like Error 500 there was an error in the script it means you have mis-typed something or the server cannot find the Python program. Error 404 means that you have typed the wrong address and that is not where the hello.cgi script is. Error 403 means you do not have permission to execute the file and you need to change the file permissions.

If you have problems you can often access your script using telnet. Load telnet and type open yourserver.com enter your username and password and navigate to your cgi script using the following commands:

ls to give a list of directories cd cgi-bin to change directory to cgi-bin cd .. to go up a level.

You can then type python hello.cgi and if there are errors in your script, the Python interpreter will find them. Also you can type which python to find out about the Python version you are using.

It can be a complicated business to start with but once you have done it once you should be able to do it again with ease!

See About this document... for information on suggesting changes.