2

Guidelines for setting up a Python file at DreamHost

 3 years ago
source link: https://help.dreamhost.com/hc/en-us/articles/216128557-Guidelines-for-setting-up-a-Python-file-at-DreamHost
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Guidelines for setting up a Python file at DreamHost

Overview

When creating a Python script at DreamHost, there are certain rules that must be followed to ensure the file functions properly.

File extensions

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

  • .py
  • .cgi
  • .fcgi

First line of the Python file

The very first line of the file could be the server location of Python 3.

#!/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 will look 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.

Viewing output

If you want to view 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 to use Python 3 parenthesis when creating a print statement.

Hello World! example

The code below is will display Hello World! in a web browser. Make sure to change username to your Shell user.

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

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

Apache execution

If you don't want .py files to be executed by Apache add the following to your .htaccess file:

RemoveHandler .py

Unix group

At DreamHost, all Python CGI scripts AND their immediate parent directory must have their Unix user and Unix group left set to your domain's Unix user AND that user's DEFAULT Unix group (otherwise you will get an internal server error from Apache's suexec).

See also

Did this article answer your questions?


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK