...

View Full Version : Using Python for CGI



Dean440
04-13-2010, 09:06 PM
I'm having some trouble getting Python working for server-side scripts. Admittedly, I'm a complete CGI and Python n00b.

So far, I've just been using PHP's passthru() function to execute Python, but I know you can do it directly.

I've tried this on my GoDaddy server (which is just a $5 economy plan, so that may be why) and on my local machine using LAMPP in htdocs and cgi-bin with no luck.

My code:


#!/usr/bin/python
print("Content-type: text/html\n\n")
print("<h1>Hello.</h1>")


I've tried "Content-Type" as well. I always get a plain-text representation of the entire source file. I've tried doing chmod a+x to make the python script executable, too. What's the right way to do this?

oracleguy
04-13-2010, 09:58 PM
At least as far as your local server, is mod_python being loaded by Apache?

Dean440
04-13-2010, 11:03 PM
At least as far as your local server, is mod_python being loaded by Apache?

My knowledge of Apache is pretty poor.

How can I tell if it's being loaded?

I searched the directory where lampp is stored and couldn't find anything about mod_python.

oracleguy
04-14-2010, 11:53 PM
Look in your httpd.conf where it is loading modules (you can tell by all the LoadModule directives), is there one for mod_python.so? If it isn't there double check any other configuration files that httpd.conf is including or just search your system for mod_python.so to see if it even exists.

What distro are you on?

Dean440
04-15-2010, 02:58 PM
I got the problem cleared up by (silly me) changing my scripts to be executable with chmod.

Samhain13
04-17-2010, 11:00 PM
My code:

#!/usr/bin/python
print("Content-type: text/html\n\n")
print("<h1>Hello.</h1>")

I've tried "Content-Type" as well. I always get a plain-text representation of the entire source file. I've tried doing chmod a+x to make the python script executable, too. What's the right way to do this?

I don't know about the right way and the wrong way, but what I do (basically) is:


#!/usr/bin/python

# Start printing headers.
print "Status: 200 OK"
print "Content-Type: text/plain"

# Print an empty line to end the headers.
print

# Print the page.
print """
<html>
<head>....

(more of the page here)

...</body>
</html>
"""



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum