# 4.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.py:

    #!/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: One issue which sometimes casues problems is line endings. UNIX uses LF as a line feed, windows uses CR+LF. 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`. Note that in this case we used two line ending characters `\n\n` after the HTTP header the second of these leaves a completely blank line which tells the server or browser the HTTP headers have finished and to expect some content. If we had many HTTP headers only the last would be sent with two blank lines. HTTP headers must be sent to the web browser before the main content of a page otherwise you are likely to get a message saying `"Server Error 500"`.

The last line prints the HTML body to the web browser and is what would be seen if you were to use your browser's View Source option.

Once you have written your script you 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`. Note: In the case of the webserver which comes with PythonWeb the cgi directory is `doc/src/lib` so you should put your application in there.

Next you will need to change the file permissions of your script using the UNIX command `chmod 755 hello.py` 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.py 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 is likely 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.py script is. Error 403 means you do not have permission to execute the file and you need to change the file permissions. If you simply see the source code it means the server is treating the file as a plain text file and not as a computer program. Be sure you have put the put the script in an executable directory.

If you have problems you can 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.py` and if there are errors in your script, the Python interpreter will find them. Correct the errors and then try to execute `./hello.py` If the script will not execute check the permisssions and check the first line correctly points to the python interpreter. You may also want to check you are using the correct line endings as mentioned earlier. Once this works the webserver should also be able to execute your script. Occasionally you may also need to change the user of the script but this is very rare.

As a final resort if you cannot get Python to work you could try and write a CGI script in perl or rename the file hello.cgi rather than hello.py. If you are still having problems it would be worth contacting your ISP and asking for some help.

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!