Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm just starting out in Python. The Python interpreter works from the command line (I have 2.4.3), but I can't seem to get Apache to execute Python scripts. All I end up with is a blank screen and nothing in the Apache error logs.

I enabled Python via the Plesk control panel. Here's the snippet that was generated in the httpd.include:

<Files ~ (\.py$)>
    SetHandler python-program
    PythonHandler   mod_python.cgihandler
</Files>

My test script is one of the examples that comes with the Python downloads at http://python.org/download/

#!/usr/local/bin/python

"""CGI test 1 - check server setup."""

# Until you get this to work, your web server isn't set up right or
# your Python isn't set up right.

# If cgi0.sh works but cgi1.py doesn't, check the #! line and the file
# permissions.  The docs for the cgi.py module have debugging tips.

print("Content-type: text/html")
print()
print("<h1>Hello world</h1>")
print("<p>This is cgi1.py")

That wasn't working, so I changed #!/usr/local/bin/python to #!/usr/bin/python which is what which python tells me but the results were the same.

Like I said, I'm ending up with a blank page. No errors that I know of, unless I'm checking the wrong error log (I'm checking the Apache error log). I'm on a MediaTemple (dv) running CentOS.

share|improve this question

2 Answers 2

up vote 2 down vote accepted

Try by replacing the code:

print("Content-type: text/html")
print()

to

print("Content-type: text/html")
print("\n\n")

Edit

print() => will print () if you run from the terminal

print => will print a line break character '\n' . See the doc here (Look up for print)

share|improve this answer
    
That works, thanks! So does all output have to be prefaced by print("Content-type: text/html") if I want it to show up in the browser? –  Justin Johnson Mar 15 '10 at 21:01
    
Yes, if you want to return some HTML, you have to set it as text/html, if you want some plain text, the content-type is text/plain . See a list of content type at en.wikipedia.org/wiki/Mime_type –  ccheneson Mar 15 '10 at 21:04
    
Cool thanks. Coming from PHP, it's strange/unexpected to have to set a content type. –  Justin Johnson Mar 15 '10 at 21:49
    
It's because "text/html" is set by default in php. You can see it in php.ini. In this file it has the following set: default_mimetype = "text/html" –  ccheneson Mar 15 '10 at 22:22

A.) Make sure you can run your script from the command line. B.) Try adding an extra print() after the first print().

share|improve this answer
    
Yup, it works from the command line –  Justin Johnson Mar 15 '10 at 20:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.