PDA

View Full Version : Just trying to get Python CGI to work!



woolhiser
07-17-2009, 05:05 PM
I had read this thread:

http://forums.westhost.com/showthread.php?t=9210&highlight=python

but it may be out of date, when I made such changes to my httpd.conf file, it crashed my appache, and I needed tech support to help me re-start it.

At this point, all I want is a working "Hello World". for an index.py file.

I have chmod 755, and set options ExecCGI in my .htaccess but I'm still getting the helpless "Internal Server Error".

Due to my working life, I've become more comfortanly in Python than PHP, but if I have to, I'll learn PHP.

Python works fine when I connect via ssh, so it's installed. But I can't get appache to swallow it.

it's a pitty though, I just love using regex in Python.

woolhiser
07-18-2009, 06:39 AM
The first thing to do was to get the shell to run in ssh using PuTTY.



#! /usr/bin/env python
print "<HTML><BODY>Hello World!</BODY></HTML>"


Well this wouldn't work from the shell, because oddly enough, "." was not in my path!

so :


$ export PATH=.:$PATH

helps and now my script runs in the shell.

There was some discussion about #! /user/bin/env python, but this standard path is working for me. Anyway, I thought I'd pass that along as I try to get the simplest of things to work.

I'm still getting an internal server error from apache, but now I know that the script should work provided apache is configured correctly (which it apparently isn't)

woolhiser
07-18-2009, 08:24 AM
Just kidding, but I have the solution for Python scripting.

It helps a lot if you read your error log found in /var/log/httpd . And there it told me exactly what was wrong!

But here's all anyone needs to do:

1) install your python using the site manager
2) in the directory where you run your python, be sure to add a .htaccess file and include:


options ExecCGI


3) create you file such as index.py:


#! /usr/bin/env python

print "Content-Type: text/html\n\n"
print "<HTML><BODY>Hello world!</BODY></HTML>"

Here, it was important to have the content type header. When I had read the /var/log/httpd/error_log, it clearly stated that the header was malformed. In addition, it received "<HTML><BODY> ..." as the header which told me that in fact, Apache was executing python, and the script was working, but the script was not producing a valid header.

4) Be sure to chmod 755 index.py . You must make your script executable.

And that's really all there is!

ecs1749
09-05-2012, 11:36 AM
Just kidding, but I have the solution for Python scripting.

It helps a lot if you read your error log found in /var/log/httpd . And there it told me exactly what was wrong!

But here's all anyone needs to do:

1) install your python using the site manager
2) in the directory where you run your python, be sure to add a .htaccess file and include:


options ExecCGI


3) create you file such as index.py:


#! /usr/bin/env python

print "Content-Type: text/html\n\n"
print "<HTML><BODY>Hello world!</BODY></HTML>"

Here, it was important to have the content type header. When I had read the /var/log/httpd/error_log, it clearly stated that the header was malformed. In addition, it received "<HTML><BODY> ..." as the header which told me that in fact, Apache was executing python, and the script was working, but the script was not producing a valid header.

4) Be sure to chmod 755 index.py . You must make your script executable.

And that's really all there is!

I followed the instructions listed and was able to get things working for one folder in the www branch but not the opt branch. Checked all the settings and seems ok but the Python program doesn't get executed - just listed. Works if I move it to under www. Don't understand why.

wildjokerdesign
09-06-2012, 06:59 AM
I think the issue is the #2 step.

2) in the directory where you run your python, be sure to add a .htaccess file and include:
You need to also add that .htaccess file to the directory you are placing the python file in. Did you do that? The reason it works in www is because the options ExecCGI is set in the httpd.conf file for that directory. :)