0

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?

1
  • Just to clarify: I know the alternatives and how to set up python properly. Just wondering why this configuration does not work... Commented Apr 28, 2009 at 19:00

4 Answers 4

13

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

Sign up to request clarification or add additional context in comments.

2 Comments

Right, but I didn't want the 'ordinary' cgi script, but avoid the interpreter line.
But php CGI is 'ordinary' cgi. You're calling the php interpreter from apache to execute your script.
5

" 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.

7 Comments

Okay, I read some more detailed articles about CGI now and think I see the point.
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.
Python runs in CGI, but, not in the same configuration that PHP does.
@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.
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.
|
1

The error "Premature end of script headers:" can occur if the .py file was edited in a Windows program that uses CRLF characters for line breaks instead of the Unix LF.

Some programs like Dreamweaver have line-break-type in preferences. Notepad also uses CRLF.

If your host has a file editor, you could test by backspacing the current linebreaks and reentering them through that editor which would change any CRLF to LF only. Notepad++ can use LF only.

Comments

0

When you open for example http://localhost/test.py you expect that Apache will somehow start process /usr/bin/python /var/www/test.py (i.e the interpreter with single command line argument). But this is not what happens because Apache calls the cgi script with no arguments. Instead it provides all the information through environment variables which are standardized by CGI.

As others have pointed out using python as plain cgi is inefficient but if for educational reasons you would still like to do it you can try this.

Assuming that the default Apache cgi-bin settings are active you can create a simple wrapper named python (or whatever you choose) in your /usr/lib/cgi-bin with the following contents:

#!/usr/bin/python
import os
execfile(os.environ['PATH_TRANSLATED'])

Don't forget to make it executable: chmod a+x /usr/lib/cgi-bin/python

Put these in you Apache config:

AddType application/python .py
Action application/python /cgi-bin/python

Now when you open http://localhost/test.py Apache will execute /cgi-bin/python with no arguments but with filled in CGI environment variables. In this case we use PATH_TRANSLATED since it points directly to the file in the webroot.
Calling execfile interprets that script inside the already opened python process.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.