IntroductionΒΆ

The code on this tutorial only works with Python 2.x. To make it run on 3.x it is usually only necessary to change the print commands to the new function syntax.

Some host providers only let you run CGI[1] scripts in a certain directory, often named cgi-bin. In this case all you have to do to run the script is to call it like this:

http://my_server.tld/cgi-bin/my_script.py

The script will have to be made executable by “others”. Give it a 755 permission or check the executable boxes if there is a graphical FTP interface.

Some hosts let you run CGI scripts in any directory. In some of these hosts you don’t have to do anything to configure the directories. In others you will have to add these lines to a file named .htaccess in the directory you want to run CGI scripts from:

Options +ExecCGI
AddHandler cgi-script .py

If the file does not exist create it. All directories below a directory with a .htaccess file will inherit the configurations. So if you want to be able to run CGI scripts from all directories create this file in the document root.

If you are using your own server then probably you won’t need to do anything to run a CGI script at the cgi-bin directory. Just make sure there is a line like the next in httpd.conf and that it is not commented. The trailing slashs are required.

ScriptAlias /cgi-bin/ "/path/to/cgi-bin/directory/"

If you are using the line above and want html files to be handled correctly in the cgi-bin directory add the next to httpd.conf. No trailing slash.

<Directory /path/to/cgi-bin/directory>
   AddHandler default-handler .html .htm
</Directory>

To run a script saved at the root:

http://my_server.tld/my_script.py

If it was saved in some directory:

http://my_server.tld/some_dir/some_subdir/my_script.py

If your desktop is the server then execute it like this:

http://localhost/cgi-bin/my_script.py

In Windows, sometimes Apache will listen on port 8080. In this case the above address will be written with the port:

http://localhost:8080/cgi-bin/my_script.py

Make sure all text files you upload to the server are uploaded as text (not binary), specially if you are in Windows, otherwise you will have problems.

[1]Common Gateway Interface