tags:

views:

1324

answers:

3

Hi, I think one commonly known way of adding PHP to an Apache webserver is to configure it like this:

ScriptAlias /php5.3 /usr/local/php5.3/bin
Action application/php5.3 /php5.3/php-cgi
AddType application/php5.3 .php

Now I tried to write a similar configuration for Python:

ScriptAlias /python /usr/bin
Action application/python /python/python
AddType application/python .py

I have a small test script that looks like this:

print "Content-Type: text/html\n\n"
print "Test"

But something seems to be wrong since the apache error log says the following:

Premature end of script headers: python

So my first though was that my python response is not right. But there is the Content-Type and also both linebreaks. Also the output of a similar PHP script called with php-cgi gives exactly the same output.

Also I haven't found a tutorial that shows how to get python working this way. So maybe it is not possible, but then I'm curious why this is the case? Or am I missing something?

+4  A: 

" So maybe it is not possible, but then I'm curious why this is the case?"

Correct. It's not possible. It was never intended, either.

Reason 1 - Python is not PHP. PHP -- as a whole -- expects to be a CGI. Python does not.

Reason 2 - Python is not inherently a CGI. It's an interpreter that has (almost) no environmental expectations.

Reason 3 - Python was never designed to be a CGI. That's why Python is generally embedded into small wrappers (mod_python, mod_wsgi, mod_fastcgi) which can encapsulate the CGI environment in a form that makes more sense to a running Python program.

S.Lott
Okay, I read some more detailed articles about CGI now and think I see the point.
okoman
Python runs perfectly fine in cgi, once you take into account the overhead of the interpreter startup on each request. Granted, I wouldn't run a modern website with it, but it does work.
JimB
Python runs in CGI, but, not in the same configuration that PHP does.
S.Lott
@S.Lott - I was just trying to clarify for the OP. PHP does run a little better as a cgi, but the standard is to use mod_php, or fastCGI. PHP can be built as a GCI binary, but it's not recommended, and not supported by most hosting providers.
JimB
With a config like that above I managed to change the PHP interpreter version by accessing the server with a different port. (http://...:80/test.php -> PHP 5.2 ; http://...:85/test.php -> PHP 5.3)My hope was to get the same working with Python. An ordinary CGI script is not the right choice since the interpreter version shall be chosen automatically. Think I'm gonna have a look at FastCGI or mess around with mod_python, although it seems to need a bit more of doc reading :-)
okoman
Mess around with mod_wsgi before you mess around with anything else. It works well and is the current state of the art for Apache-Python interaction.
S.Lott
because the simplest way to call a CGI is through a shell, you can change the shebang on your python CGI script to point to a different python version. I also would recommend mod_wsgi as the best method for deployment right now.
JimB
+5  A: 

You can use any type of executable as cgi. Your problem is in your apache config, which looks like you just made it up. Check the apache docs for more details, but you don't need the Action and AddType.

ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"

Then drop the following into your cgi-bin:

#!/usr/bin/python
# test.py
print "Content-Type: text/html\n\n"
print "Test"

Make sure it's executable, and see the result at /cgi-bin/test.py

JimB
Nice and concise answer.
Jarret Hardie
Right, but I didn't want the 'ordinary' cgi script, but avoid the interpreter line.
okoman
But php CGI is 'ordinary' cgi. You're calling the php interpreter from apache to execute your script.
JimB
A: 

Hi, have a problem in configuring cgi in xampp apache. it display the entire codes of python enterpreter. Can anyone help me please!!!.. tnx :)

unknown
If you want to ask a question, don't put it in an answerbut start a new thread for it.The "Ask Question" button ins in the top right of this page.More people would see your question and try to answer that way.Make sure to provide as many details as possible. For example post the configuration settings you are having problems with.
sth