By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,617 Members | 1,708 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,617 IT Pros & Developers. It's quick & easy.

Apache web server and CGI

P: n/a
Lad
How can I set up Apache web server to use Python for CGI processing( for file *.py).
Thanks for help
Lad
Jul 18 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Lad wrote:
How can I set up Apache web server to use Python for CGI processing( for
file *.py). Thanks for help


A simple method would be to include the line:

AddHandler cgi-script .py

in your httpd.conf. This should work fine so long as your .py files start
with a proper invocation of the python interpreter (i.e.,
#!/usr/bin/python). You might also want to look at mod_python
(http://www.modpython.org/).

Jeffrey
Jul 18 '05 #2

P: n/a
In article <81*************************@posting.google.com> , Lad wrote:
How can I set up Apache web server to use Python for CGI processing( for file *.py).
Thanks for help
Lad


I will assume you have cgi working in apache in cgi-bin. I will also assume
that you want to use it on *NIX. You didn't tell and I don't know how to do
it on windows ;-)

The simplest thing that should work is sth like this:

Create a file: test.py

---------------------------------------
#!/usr/bin/env python

print "Content-Type: text/html"
print
print """
<html>
<head>
<title></title>
</head>
<body>
Hello Lad!
</body>
</html>
"""
----------------------------------------

Now do:

chmod 755 test.py

And then go there with a browser:

lynx http:/localhost/cgi-bin/test.py

Hth,

PterK

--
Peter van Kampen
pterk -- at -- datatailors.com
Jul 18 '05 #3

P: n/a
Lad
Jeffrey Froman <je*****@I.slack> wrote in message news:<10*************@corp.supernews.com>...
Lad wrote:
How can I set up Apache web server to use Python for CGI processing( for
file *.py). Thanks for help


A simple method would be to include the line:

AddHandler cgi-script .py

in your httpd.conf. This should work fine so long as your .py files start
with a proper invocation of the python interpreter (i.e.,
#!/usr/bin/python). You might also want to look at mod_python
(http://www.modpython.org/).

Jeffrey


Thank you Jeffrey and Peter for help.
I added AddHandler cgi-script .py to httpd.conf
I also tried to run the test script as Peter suggested but the Apache
says( in error log)

[Tue Aug 10 08:51:32 2004] [error] [client 127.0.0.1] (2)No such file
or directory: script not found or unable to stat: c:/program
files/apache group/apache/cgi-bintest.py

The name of script is test.py and is in cgi-bin (Apache subdirectory).

I tried to run the script from IE browser with the command
http://localhost/cgi-bin/test.py

but without success. I use XP home edition.

Apache runs without problem as I could check it by inserting
http://localhost/

BTW, how can I set up a different directory for cgi scripts? For
example I would like to have all my scripts in
C:\Scripts\MyScripts

Thank you for help
LAd
Jul 18 '05 #4

P: n/a
In article <81************************@posting.google.com>, Lad wrote:
[Tue Aug 10 08:51:32 2004] [error] [client 127.0.0.1] (2)No such file
or directory: script not found or unable to stat: c:/program
files/apache group/apache/cgi-bintest.py

The name of script is test.py and is in cgi-bin (Apache subdirectory).
In my experience cgi-bin is rarely just a subdir of apache's www-root. The
name cgi-bin is in itself not some magical name that 'turns CGI on'. You
have to configure it on your httpd.conf. Here's a relevant bit of mine:

#
# ScriptAlias: This controls which directories contain server scripts.
# ScriptAliases are essentially the same as Aliases, except that
# documents in the realname directory are treated as applications and
# run by the server when requested rather than as documents sent to the client.
# The same rules about trailing "/" apply to ScriptAlias directives as to
# Alias.
#
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/

#
# "/usr/lib/cgi-bin" could be changed to whatever your ScriptAliased
# CGI directory exists, if you have that configured.
#
<Directory /usr/lib/cgi-bin/>
AllowOverride None
Options ExecCGI
Order allow,deny
Allow from all
</Directory>

Also just to be sure the first line of test.py should be sth like this on
windows:

#! c:\path\to\python.exe
BTW, how can I set up a different directory for cgi scripts? For
example I would like to have all my scripts in
C:\Scripts\MyScripts


This is probably clear by now...

Hth,

PterK

--
Peter van Kampen
pterk -- at -- datatailors.com
Jul 18 '05 #5

P: n/a
Lad
Peter van Kampen <ne**@woody.datatailors.com> wrote in message news:<slrnchhcv5.mtn.ne**@woody.datatailors.com>.. .
In article <81************************@posting.google.com>, Lad wrote:
[Tue Aug 10 08:51:32 2004] [error] [client 127.0.0.1] (2)No such file
or directory: script not found or unable to stat: c:/program
files/apache group/apache/cgi-bintest.py

The name of script is test.py and is in cgi-bin (Apache subdirectory).


In my experience cgi-bin is rarely just a subdir of apache's www-root. The
name cgi-bin is in itself not some magical name that 'turns CGI on'. You
have to configure it on your httpd.conf. Here's a relevant bit of mine:

#
# ScriptAlias: This controls which directories contain server scripts.
# ScriptAliases are essentially the same as Aliases, except that
# documents in the realname directory are treated as applications and
# run by the server when requested rather than as documents sent to the client.
# The same rules about trailing "/" apply to ScriptAlias directives as to
# Alias.
#
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/

#
# "/usr/lib/cgi-bin" could be changed to whatever your ScriptAliased
# CGI directory exists, if you have that configured.
#
<Directory /usr/lib/cgi-bin/>
AllowOverride None
Options ExecCGI
Order allow,deny
Allow from all
</Directory>

Also just to be sure the first line of test.py should be sth like this on
windows:

#! c:\path\to\python.exe
BTW, how can I set up a different directory for cgi scripts? For
example I would like to have all my scripts in
C:\Scripts\MyScripts


This is probably clear by now...

Hth,

PterK

Thanks Peter for help. Now I can run cgi scripts in default
c:/program files/apache group/apache/cgi-bin/
directory.
The problem was that there was not last "/"

But how can I forced the Apache to allowed to run scripts in a
different directory for example C:\Test\MyScripts\
Thanks for help
Lad
Jul 18 '05 #6

P: n/a
In article <81*************************@posting.google.com> , Lad wrote:
<Directory /usr/lib/cgi-bin/>
AllowOverride None
Options ExecCGI
Order allow,deny
Allow from all
</Directory>
Thanks Peter for help. Now I can run cgi scripts in default
c:/program files/apache group/apache/cgi-bin/
directory.
The problem was that there was not last "/"

But how can I forced the Apache to allowed to run scripts in a
different directory for example C:\Test\MyScripts\


I would guess you have something like the above in your httpd.conf

ScriptAlias /cgi-bin/ c:/program files/apache group/apache/cgi-bin/

<Directory c:/program files/apache group/apache/cgi-bin/>
AllowOverride None
Options ExecCGI
Order allow,deny
Allow from all
</Directory>

Simply replace c:/program files/apache group/apache/cgi-bin/ with
C:\Test\MyScripts

Hth,

PterK
--
Peter van Kampen
pterk -- at -- datatailors.com
Jul 18 '05 #7

P: n/a
Lad
Peter van Kampen <ne**@woody.datatailors.com> wrote in message news:<slrnchk875.p2u.ne**@woody.datatailors.com>.. .
In article <81*************************@posting.google.com> , Lad wrote:
<Directory /usr/lib/cgi-bin/>
AllowOverride None
Options ExecCGI
Order allow,deny
Allow from all
</Directory>

Thanks Peter for help. Now I can run cgi scripts in default
c:/program files/apache group/apache/cgi-bin/
directory.
The problem was that there was not last "/"

But how can I forced the Apache to allowed to run scripts in a
different directory for example C:\Test\MyScripts\


I would guess you have something like the above in your httpd.conf

ScriptAlias /cgi-bin/ c:/program files/apache group/apache/cgi-bin/

<Directory c:/program files/apache group/apache/cgi-bin/>
AllowOverride None
Options ExecCGI
Order allow,deny
Allow from all
</Directory>

Simply replace c:/program files/apache group/apache/cgi-bin/ with
C:\Test\MyScripts

Hth,

PterK


Peter, Thanks a lot. Now it works, but the scripts must be still
started as
http://localhost/cgi-bin/test2.py eventhough the scripts are not
/cgi-bin/ directory but in MyScripts directory. I think that Apache
maps MyScripts directory to /cgi-bin/ directory.
Thanks again for help
Lad
Jul 18 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.