Samir Samir - 4 months ago 6x
Python Question

Basic python script giving Internal Server Error

I'm using hostgator and they swear that python is supported, but I've yet to see one working python script. I've tried several scripts, my latest one is the one they give on their website:

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'm getting an Internal Server Error:

The file has 755 permissions, so does the folder cgi-bin. They refused to help me with what they call "coding" problems... but with all that I've tried and received nothing but Internal Server Errors, I'm thinking the problem is with them. Anyone know what could be wrong?

Error Logs:

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

I've talked with my host (hostgator) and they've turned it into a ticket, which makes me think that something is up on their end.


There is documentation on how to use Python as a CGI.

In your case, the issue was the line endings. They're the hidden character at the end of each line.

On a Unix-like system, The line endings in the program file must 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 specified there. It gets easily confused by Windows line endings (Carriage Return & Line Feed, also called CRLF), so you have to convert the file to Unix line endings (only Line Feed, LF). This can be done automatically by uploading the file via FTP in text mode instead of binary mode, but the preferred way is just telling your editor to save the files with Unix line endings. Most editors support this.

On Windows (ie Notepad) they're represented as (CR LF, or \r\n), whereas on unix they're (LF or \n).

In Notepad++, you can view line endings as symbols like so: View -> Show Symbol -> Show End of Line

To replace windows linebreaks with unix ones, you can use: Search -> Replace...

In the replace dialogue, find \r\n, and replace them with \n. Ensure the Extended Search mode is selected.

Notepad++ replace dialogue screenshot

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

Notepad++ EOL Conversion screenshot