The easiest way to get started with Python on a server is with CGI. Really the only difference between CGI and running the script on your home computer is CGI needs to output a "Content-Type" header.
Open the File Manager in cPanel
In the top left click
test.py as your filename
Create New File
test.py file and click
Check execute on all three columns so it says
reload and make sure the
755 shows up under the permissions column
test.py and click
UTF-8 as the encoding and click
Copy/paste this in:
#!/usr/bin/python3.7print("Content-Type: text/html\n\n")print("Heliohost rules!")
If everything worked it should say
Heliohost rules! in your browser. The first line is the shebang. That's what tells the server which version of python to use. On Tommy and Johnny you can pick between:
python3.6 for Ricky.
The next line is the
Content-Type header. This is important so the server knows what to do with the output that follows. If you forget it your script will give a useless 500 error. The content type header always has to have the two end lines immediately after it. That's what the
You can see the modules that are currently installed on the Python versions:
If you don't see the module you need your script will probably give a useless 500 error when you try to import the missing module. Just make a post on the forums stating your server, the version of python you're using, and the module(s) you need.
Executing a CGI script outside the
cgi-bin folder is possible with some modifications.
Create a file named
.htaccess and copy/paste this in:
Options +ExecCGIAddHandler cgi-script .pyDirectoryIndex index.py
The first two lines make
.py files executable outside
cgi-bin, and the last line makes the filename
index.py show up if someone goes to your domain without having to type out the filename like
domain.heliohost.org/index.py. This also supports other CGI file extensions.