Basic python script giving Internal Server Error
Question: 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:

    
    
    #!/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'm getting an Internal Server Error: <http://elkuzu.com/cgi-bin/test.py>

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


Answer: There is [documentation](http://docs.python.org/howto/webservers.html) 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++](http://notepad-plus-plus.org/), 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](http://i.stack.imgur.com/vZpwZ.png)

* * *

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

![Notepad++ EOL Conversion screenshot](http://i.stack.imgur.com/POxEV.png)

