Basic python script giving an internal server error

I use hostgator and they swear that python is supported, but I have not seen one working python script yet. I tried several scripts, my last one is the one they give on their website:

#!/usr/bin/python print "Content-type: text/html\n\n"; print "<html><head>"; print "<title>CGI Test</title>"; print "</head><body>"; print "<p>Test page using Python</p>"; print "</body></html>"; 

I get an internal server error: http://elkuzu.com/cgi-bin/test.py

The file has 755 permissions, also has a cgi-bin folder. They refused to help me with what they call the "coding" of problems ... but with all that I tried and received nothing but errors from the internal server, I think the problem is with them. Does anyone know what could be wrong?

Error Logs:

 [Sat May 19 09:11:38 2012] [error] [client 74.129.48.242] File does not exist: /home/elkuzu/public_html/404.shtml [Sat May 19 09:11:38 2012] [error] [client 74.129.48.242] File does not exist: /home/elkuzu/public_html/favicon.ico [Sat May 19 09:11:37 2012] [error] [client 74.129.48.242] File does not exist: /home/elkuzu/public_html/500.shtml 

I talked with my hostgator, and they turned it into a ticket, which makes me think that something is on their side.

+4
source share
2 answers

There is documentation on how to use Python as CGI.

In your case, the problem was the end of the line. This is a hidden character at the end of each line.

On a Unix-like system, the line ending in the program file should be Unix-style line endings. This is important because the web server checks the first line of the script (called shebang) and tries to run the program there. It easily confuses the Windows line (Carriage Return and Line Feed, also called CRLF), so you have to convert the file to the end of a Unix line (Line Feed, LF only). This can be done automatically by uploading the file via FTP in text mode instead of binary mode, but the preferred way is to simply tell the editor to save the files with the end of the Unix line. Support for most editors is.

On Windows (e.g. Notepad) they are represented as (CR LF, or \r\n ), while on unix they are (LF or \n ).

In Notepad ++, you can view strings as characters as follows: View → Show Symbol → Show End of Line

To replace Windows line breaks with unix, you can use: Search -> Replace ...

In the replacement dialog, find \r\n and replace them with \n . Make sure that the advanced search mode is selected.

Notepad ++ replace dialogue screenshot


Alternatively you can use Edit -> EOL Conversion -> UNIX Format

Notepad ++ EOL Conversion screenshot

+7
source

I know this is an old question, but I also encountered a 500 error using the host giant (to indicate that I stopped trying a year ago). A few days ago, I decided to try again after my “ textbook ”, and it did not work again.

I will try something simpler:

 #!/usr/bin/python import sys print sys.version_info 

I got 500 errors again, but after a conversation with support, they gave a log message saying:

[error] [client 192.xxx] invalid header from script. Bad title = (2, 6, 6, 'final', 0): test1.py

With this information (the log message contained python, so it somehow ran) I found this post on the Unix site where I got this:

As the HTTP specification (and the error message from your HTTP server) indicates, you need one blank line between the HTTP headers and the body, otherwise the server does not know where the headers end and the body begins.

I changed my script to:

 #!/usr/bin/python print "" import sys print sys.version_info 

And it worked !. Oddly enough, after the successful execution of this script, the example in the tutorial (code below) now works for me as well

 #!/usr/bin/python print "Content-type: text/html\n\n"; print "<html><head>"; print "<title>CGI Test</title>"; print "</head><body>"; print "<p>Test page using Python</p>"; print "</body></html>"; 

Why does it work now? I have no idea, but at least I hope this answer helps someone who is dealing with a lack of hostgator support.

0
source

Source: https://habr.com/ru/post/1413315/


All Articles