Set - 8

Question 1 :

How do I use Python for CGI?

Answer :

On the Microsoft IIS server or on the Win95 MS Personal Web Server you set up Python in the same way that you would set up any other scripting engine. 
Run regedt32 and go to:


and enter the following line (making any specific changes that your system may need):

.py :REG_SZ: c:\\python.exe -u %s %s

This line will allow you to call your script with a simple reference like: http://yourserver/scripts/ provided "scripts" is an "executable" directory for your server (which it usually is by default). The "-u" flag specifies unbuffered and binary mode for stdin - needed when working with binary data. 
In addition, it is recommended that using ".py" may not be a good idea for the file extensions when used in this context (you might want to reserve *.py for support modules and use *.cgi or *.cgp for "main program" scripts). 
In order to set up Internet Information Services 5 to use Python for CGI processing, please see the following links: (for Win2k Server) (for Win2k pro) 
Configuring Apache is much simpler. In the Apache configuration file httpd.conf, add the following line at the end of the file:

ScriptInterpreterSource Registry

Then, give your Python CGI-scripts the extension .py and put them in the cgi-bin directory.

Question 2 :

How do I emulate os.kill() in Windows? 

Answer :

Use win32api: 
def kill(pid):
"""kill function for Win32"""
import win32api
handle = win32api.OpenProcess(1, 0, pid)
return (0 != win32api.TerminateProcess(handle, 0))


Question 3 :

Why does os.path.isdir() fail on NT shared directories?

Answer :

The solution appears to be always append the "\" on the end of shared drives.

>>> import os
>>>os.path.isdir( '\\\\rorschach\\public')
>>>os.path.isdir( '\\\\rorschach\\public\\')

It helps to think of share points as being like drive letters. Example:

k: is not a directory
k:\ is a directory
k:\media is a directory
k:\media\ is not a directory

The same rules apply if you substitute "k:" with "\conkyfoo":

\\conky\foo is not a directory
\\conky\foo\ is a directory
\\conky\foo\media is a directory
\\conky\foo\media\ is not a directory

Question 4 :

Web Python ?

Answer :

Some host providers only let you run CGI 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: 
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 do 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. 

To run a script saved at the root: 
If it was saved in some directory: 
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.

Question 5 :

The classical "Hello World" in python CGI fashion?

Answer :

#!/usr/bin/env python
print "Content-Type: text/html"
print """\
<h2>Hello World!

To test your setup save it with the .py extension, upload it to your server as text and make it executable before trying to run it. 
The first line of a python CGI script sets the path where the python interpreter will be found in the server. Ask your provider what is the correct one. If it is wrong the script will fail. Some examples: 


It is necessary that the script outputs the HTTP header. The HTTP header consists of one or more messages followed by a blank line. If the output of the script is to be interpreted as HTML then the content type will be text/html. The blank line signals the end of the header and is required.

print "Content-Type: text/html"

If you change the content type to text/plain the browser will not interpret the script's output as HTML but as pure text and you will only see the HTML source. Try it now to never forget. A page refresh may be necessary for it to work.

Client versus Server 
All python code will be executed at the server only. The client's agent (for example the browser) will never see a single line of python. Instead it will only get the script's output. This is something realy important to understand. 

When programming for the Web you are in a client-server environment, that is, do not make things like trying to open a file in the client's computer as if the script were running there. It isn't.