Notes on Using Python for CGI Scripts under Apache

I've been using PHP for so long that I haven't had much call to delve into the CGI-BIN directory in quite a while. Once I determined that my host has Python installed, I decided to see if I can run Python scripts from the CGI-BIN directory. It can be done, but right off the bat I ran into a problem whose cause wasn't immediately apparent.

I had a simple hello world script in the cgi-bin directory:
hello.py
#!/usr/bin/python
print "Content-Type: text/html\n\n"
print "Hello, world!"

Path to the interpreter correct, check. Permissions set to executable, check. Nevertheless, Apache kept returning an HTTP 500 error, and writing "Premature end of script headers" to the log.

Shame on me for editing the file over ange-ftp from a Windows installation of Emacs; the script was getting saved with the vexing MS-DOS style rn newlines, which was causing the error. Once I figured that out and stripped those damn ^M characters from the file in Emacs literal mode, I remember having the same problem back when I was using PERL.

Emacs-specific Stuff

With a little more digging I finally found out how to do what I've always suspected was possible, that is to configure Emacs to always create files in 'UNIX' mode even when running on Windows. Add this to your .emacs file:

(setq default-buffer-file-coding-system 'undecided-unix)

And, to fix the newline characters in an existing file, you can use

set-buffer-file-coding-system

(Usually bound to C-x RET f.) Enter 'unix' at the prompt, and re-save the file.

(Many thanks to Stephen Leake for his post at http://mail.gnu.org/archive/html/help-emacs-windows/2002-12/msg00053.html)

Syndicate content

Twitter

  • Found my misplaced Hero 329! It had been in the truck for a few weeks. #fountainpen — 5 years 3 days ago
  • Note to speakers & writers of the English language. It's D-R-A-W-E-R, not "draw". They speak english in what? — 5 years 3 days ago
  • How to compile + install xdebug for entropy.ch PHP5 on OS X Leopard: http://is.gd/4WkwW — 5 years 4 days ago
  • RT @ebertchicago: Martin Scorsese's list of the Top 11 Horror Films of All Time. http://j.mp/2AfcFX (yep, The Changling is on it) — 5 years 5 days ago
  • @krazykatmama it's OK, our neighbor saw my plight on Facebook and within 10 minutes came over and brought us cupcakes! — 5 years 5 days ago

Older

Contact

Andy Chase
(978) 297-6402
andychase [at] gmail.com
GPG/PGP Public Key