Error500 with a python script!

Error500 with a python script!

Posted by: Mitheor
Posted on: 2008-05-06 03:28:00

Hi,

i was trying to use a python script but it always returns an 500 internal server error.

The script is very simple. I have tried to use the example of the the dreamhost wiki but it doesnt work either.


"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
#!/usr/bin/python

def main():
print "Content-type: text/html"
print
print "<html><head>"
print "<title>Hello World from Python</title>"
print "</head><body>"
print "Standard Hello World from a Python CGI Script"
print "</body></html>"

if __name__ == "__main__":
main()
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""


"Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, webmaster@uguu-web.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request."


Heeeeelp! >_<

Re: Error500 with a python script!

Posted by: patricktan
Posted on: 2008-05-06 18:27:00

Do you have the access to error.log file? Please check the log and see what it says.

Most of the time, Internal Server Error is caused by syntax error. Please have ; at the end of each line of your code. And you may want to try

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

You have to have two empty lines after the header.

Re: Error500 with a python script!

Posted by: Nosgoroth
Posted on: 2008-05-07 00:27:00

Programming partner here. Error log says:

Re: Error500 with a python script!

Posted by: patricktan
Posted on: 2008-05-07 18:25:00

What is the file permission on the py file? You'll need execute permission on the file.

Re: Error500 with a python script!

Posted by: Nosgoroth
Posted on: 2008-05-08 01:12:00

It's 755. I tried with 777 too.

...but, somehow, it started working just now. Odd.

Re: Error500 with a python script!

Posted by: patricktan
Posted on: 2008-05-08 18:38:00

Strange... Now let's shake legs smile

Re: Error500 with a python script!

Posted by: rlparker
Posted on: 2008-05-08 19:59:00

Try this one:

#!/usr/bin/python
# for linux

import sys
import os

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

def printArgs():
for arg in sys.argv:
print arg + "<br/>"
def printEnv():
for key in os.environ.keys():
print "<b>" + key + "</b>=" + os.environ[key] + "<br/>"

def printVersion():
print "Python %s" % sys.version

def main():
printHeader()

print "<TITLE>Hello World from Python</TITLE>"
print "Standard Hello World from a Python CGI Script"

print "<p />Python Info...<br />"
print "<code>"
printVersion()
print "</code>"

print "<p />Command Line...<br />"
print "<code>"
printArgs()
print "</code>"

print "<p />Environment...<br />"
print "<code>"
printEnv()
print "</code>"

if( __name__ == "__main__"):
main()

Note that you need to use a *nix editor, or in some other way make sure you have proper *nix style line endings. Failure to do so will often result in the kinds of errors you are seeing. wink

--rlparker

Re: Error500 with a python script!

Posted by: Nosgoroth
Posted on: 2008-05-09 11:18:00

The 500itis is back again. Unix style lines and everything. I named the script from rlparker "tests.py". It returns a 500, and error.log says:

Re: Error500 with a python script!

Posted by: norm1037
Posted on: 2008-05-09 11:53:00

Or your webtest.py (chmod 755)

rewritten as

#!/usr/bin/python
print "Content-type: text/html\n\n"
print """
<html><head><title>Hello World from Python</title></head><body>Standard Hello World from a Python CGI
Script</body></html>
"""

should work.



--
Norm


Opinions are my own views, not DreamHosts'.
I am NOT a DreamHost employee OK! mad

Act on my advice at your own risk!

Re: Error500 with a python script!

Posted by: Nosgoroth
Posted on: 2008-05-09 12:17:00

I think you meant triple single quotes, not double. I tried with both options just in case, and it does the same thing. I don't think it's a problem of the script itself, it works with my local apache >_<

Re: Error500 with a python script!

Posted by: norm1037
Posted on: 2008-05-09 12:29:00

No I used triple double quotes and in the editor added a blank line at the end of the script.




--
Norm


Opinions are my own views, not DreamHosts'.
I am NOT a DreamHost employee OK! mad

Act on my advice at your own risk!

Re: Error500 with a python script!

Posted by: Nosgoroth
Posted on: 2008-05-09 13:10:00

Well, that doesn't work either.

Re: Error500 with a python script!

Posted by: rlparker
Posted on: 2008-05-09 18:56:00

777 will not work under suExec on DreamHost,iles to be executed as CGI need to be 755 or more restrictive. Period. Pythin *is* picky about editors, blank lines, extra characters. Etcetera.

The code I posted works fine, on a DH server, when properly prepared, upload, chmodded, etc.

http://maddogz.com/dev/python/pytest.py
http://maddogz.com/dev/python/pytest2.py

The same code for both above links, prepared with different *nix editors in the shell. wink

--rlparker

Tags: internal server errorpython script500 internal server errorusr bincontent typetext htmlwiki