Debugging Python server-side scripts

From Knowledge Kitchen
Jump to navigation Jump to search


Python debugging through a web browser is very difficult. The cgitb module I've used in my examples helps a little bit, but leaves a lot to be desired. It is easier to first debug on the server-side with the command line before trying to load the script in a web browser.

Debugging on the server-side

The basic steps to debug Python scripts on the server-side are the following:

Make sure the program runs

To verify that the program runs from the command-line, do the following:

module load python-3.5.2
cd public_html/my_folder_for_this_project
  • make sure the script has its permissions set so anyone can read and execute it
chmod o+rx my_script_file_name.py
  • execute the script on the i6 command line, e.g.:
python3 my_script_file_name.py

Verify the shebang

The first line of the python code should be the shebang, which allows the script to be executed directly. So, for example, the first line should look something like:

#!/usr/bin/env python3

And this will allow you to execute the script from the command line directly, as in:

./my_script_file_name.py

Check that the headers are output before the content

All communication from a web server to a web client must be formatted according to the HTTP protocol. HTTP responses from servers to clients must consist of HTTP response headers, followed by two line breaks, followed by the body of the response.

If the Python script is functioning correctly, you should see the HTTP headers ("Content-type" and any others) followed by two line breaks followed by the complete HTML code.

print("Content-Type: text/html;charset=utf-8")
print("\n\n")

If your script shows no errors when you run it, but outputs something before it outputs the HTTP headers, or simply doesn't output the headers at all, that is not going to be valid HTTP, and so the script won't work when you try to request it from the web browser. So make sure your script outputs those headers, and don't output anything before them!

Fix any errors

If there are errors, you should see them show up clearly on the command line.... Fix them!

If there are no errors, and your program seems to start out ok, but mysteriously stops in the middle, then you actually do have errors in your code... Fix them!

Once you've fixed any errors, you are ready to move on to client-side debugging.

Debugging on the client-side

Assuming the script works when run from the command line, it may (or may not) work when accessed from a web browser.

Make sure the program runs

Try loading the script from the web browser. E.g. Point your web browser to:

http://i6.cims.nyu.edu/~my_net_id/my_folder_for_this_project/my_script_file_name.py

If you see no errors, you are done - the server-side program is working (we hope).

Debug client-side error messages

Client-side error messages are really a result of server-side problems. But you often won't see those server-side problems until you have tried to run your scripts from a web browser.

If you see an error when loading the script from a web browser, make sure you have done the following:

  • complete all the server-side debugging steps above
  • hold down the Shift key on your keyboard and simultaneously click the "reload" button in your web browser to make sure the browser is not showing an old copy of the page
  • double-check that you are requesting the correct URL in the browser
  • double-check that your files are stored in the proper place on the server
  • make sure your files and folders all follow good file-naming conventions, and don't include any spaces, capital letters, or special characters except the underscore _ character