The Conditions for Peace
between Webservers and Python programs

The lessons learned after confronting this.  Assume a program in "Fred.py" for user "pbrooks".

  1. Did you save your program in your public_html directory (or a subdirectory under public_html) ?

  2. Did you change the file permissions to make it executable?

  3. Test it with the PythonChecker:  http://bert.stuy.edu/pbrooks/PythonChecker

  4. Did you test it in the terminal (or windows cmd window):  $./Fred.py to see if there are any syntax errors?

  5. Did you test it with url:  http://lisa.stuy.edu/~pbrooks/Fred.py ?

  6. Did you spell the filename correctly in the url? (It's Fred.py, not fred.py).

  7. The shebang (first) line in your program file should be:
    #! /usr/bin/python3

  8. Note: Everything that Fred.py prints will be sent to the user's browser

  9. The first thing that your program should do is print the content-type of the rest of the information that the program will print.  That content-type is "text/html".  So...
    print ("Content-type: text/html\n")

  10. If you get the following error message when executing the program (look for the "^M" in the error message):
    ./Fred.py: /usr/bin/python^M: bad interpreter: No such file or directory
    then the most likely cause is that you've just transferred this program file from your home Windows laptop, and Windows writes files a little differently than Unix or Mac.  In particular, it ends each line of a text file with not only the character "\n", but rather the pair of characters "\r\n".  The extra "\r" at the end of the shebang line makes the Unix shell throw up with the error message you see above.  (BTW, the "^M" is how the Unix shell displays the problematic "\r" character).  How to fix?  Execute the following line to fix your Fred.py file:
    ~pbrooks/dos2unix  Fred.py