Guidelines for setting up a Python file at DreamHost

 

Overview

This article provides guidelines for creating a Python script, which must be followed to ensure the file functions properly.

File guidelines

These sections cover what to include when creating a Python script file.

Extensions

All Python CGI scripts on DreamHost must end with the following extensions:

Extension Use case
.fcgi This extension uses FastCGI
.py This extension uses CGI
.cgi This extension uses CGI

The first line of the Python file

The very first line of the file can be the server location of Python3:

#!/usr/bin/python3

However, if you've installed a custom version of Python, the first line in your file should point to that location instead. For example, after installing a custom version of Python, run which python. The output looks like this:

[server]$ which python
/home/username/opt/python-3.8.5/bin/python3

The first line of your file should then look like this:

#!/home/username/opt/python-3.8.5/bin/python3

File permissions

The file's permissions must be set to 755. You can change this by running the following command via SSH:

[server]$ chmod 755

Line endings

Make sure to use UNIX-style newlines, not Windows.

File content guidelines

These sections cover basic information on how to view the output of your Python scripts.

Viewing the output

If you want to view the printed output from your Python code, you must add this as the first line of output:

print("Content-type: text/html\n\n")

Make sure you use print() when creating a print statement. See this page for more information about using Python3 parentheses.

Hello World! example

The following code displays Hello World! in a web browser. Make sure to change the username to your Shell user:

#!/home/username/opt/python-3.8.5/bin/python3

print("Content-type: text/html\n\n")
print("Hello World!")
 

See also

Did this article answer your questions?

Article last updated PST.

Still not finding what you're looking for?