Python Tutorial: Difference between revisions
m Update server listings to preferred order |
|||
(12 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
= Python = | == Python == | ||
The easiest way to get started with Python on a server is with CGI. The only real difference between CGI and running the script on your home computer is that CGI needs to output a | The easiest way to get started with Python on a server is with CGI. The only real difference between CGI and running the script on your home computer is that CGI needs to output a 'Content-Type' header. | ||
{{Info|We also have tutorials for building with [[:Flask|Flask]], [[:Django_on_HelioHost|creating a new Django project]], and [[:Converting_an_Existing_Django_App|converting an existing Django app to work on HelioHost]].}} | |||
{| class="wikitable" style="margin:auto" | == Python Versions Available == | ||
=== Morty Server === | |||
{| class=="wikitable" style=="margin:auto" | |||
|- | |- | ||
! Python Version !! Python Path !! Python Modules Installed | ! Python Version !! Python Path !! Python Modules Installed | ||
|- | |- | ||
| 3.9 || #!/usr/bin/python3.9 || [https:// | | 3.9 || #!/usr/bin/python3.9 || [https://krydos3.heliohost.org/pyinfo/info3.9.py View] | ||
|- | |- | ||
| 3.12 || #!/usr/bin/python3.12 || [https:// | | 3.12 || #!/usr/bin/python3.12 || [https://krydos3.heliohost.org/pyinfo/info3.12.py View] | ||
|} | |} | ||
{| class="wikitable" style="margin:auto" | === Tommy Server === | ||
{| class=="wikitable" style=="margin:auto" | |||
|- | |- | ||
! Python Version !! Python Path !! Python Modules Installed | ! Python Version !! Python Path !! Python Modules Installed | ||
Line 26: | Line 29: | ||
|} | |} | ||
= Steps = | === Johnny Server === | ||
{| class=="wikitable" style=="margin:auto" | |||
|- | |||
! Python Version !! Python Path !! Python Modules Installed | |||
|- | |||
| 3.9 || #!/usr/bin/python3.9 || [https://krydos2.heliohost.org/pyinfo/info3.9.py View] | |||
|- | |||
| 3.12 || #!/usr/bin/python3.12 || [https://krydos2.heliohost.org/pyinfo/info3.12.py View] | |||
|} | |||
== Steps == | |||
* Open the File Manager in Plesk | * Open the File Manager in Plesk | ||
* Open the | * Open the 'httpdocs' webroot directory. (If you were transferred from the old cPanel, the directory will be called 'public_html'.) | ||
* Open the | * Open the 'cgi-bin' directory | ||
* In the top left click the | * In the top left click the '+' button and select 'Create File' | ||
* Type | * Type 'test.py' as your filename | ||
* Click | * Click 'OK' to create the file | ||
* Highlight the | * Highlight the 'test.py' file and select 'Change Permissions' | ||
* Add a checkmark to the | * Add a checkmark to the 'Execute / Search' column for all three rows (Owner / Group / Others) | ||
* Click the | * Click the 'Save' button | ||
* Make sure | * Make sure 'rwx r-x r-x' ('755') is displayed under the Permissions column | ||
* Highlight | * Highlight 'test.py' and select 'Edit in Text Editor' | ||
* Leave | * Leave 'UTF-8' as the encoding, set 'Line break type' to 'Convert to Unix style' and copy/paste the below into the large text field: | ||
Line 47: | Line 61: | ||
print("Content-Type: text/html\n\n") | print("Content-Type: text/html\n\n") | ||
print(" | print("HelioHost rules!") | ||
</pre> | </pre> | ||
* Click the | * Click the 'Save' button | ||
* Highlight the | * Highlight the 'test.py' file and select 'Open in Browser' (Or manually navigate to: '<your subdomain>.helioho.st/cgi-bin/test.py') | ||
If everything worked, you should see | If everything worked, you should see 'HelioHost rules!' displayed in your browser. | ||
= Code Explanation = | == Code Explanation == | ||
In the code pasted in, the first line is called the | In the code pasted in, the first line is called the 'shebang'. It tells the server which version of Python to use. | ||
The second line of code is the | The second line of code 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 you an error. The 'Content-Type' header always has to have the two end lines immediately after it. That's what the two '\n' are. | ||
= Troubleshooting = | == Troubleshooting == | ||
If you don't see the module you need, your script will probably give you an error when you try to import the missing module. You can [[View_Error_Logs|view your account error logs]] for details on which module is missing and causing the error. | If you don't see the module you need, your script will probably give you an error when you try to import the missing module. You can [[View_Error_Logs|view your account error logs]] for details on which module is missing and causing the error. | ||
= Request Additional Python Modules = | == Request Additional Python Modules == | ||
{{Info| | {{Info| | ||
Line 73: | Line 87: | ||
}} | }} | ||
After you have checked the [[:Python_Tutorial#Python_Versions_Available|list of modules already installed]] on your server, if there is another module you'd like to request, please create a post in the [https://helionet.org/index/forum/45-customer-service/?do=add Customer Service forum]. Make sure you provide | After you have checked the [[:Python_Tutorial#Python_Versions_Available|list of modules already installed]] on your server, if there is another module you'd like to request, please create a post in the [https://helionet.org/index/forum/45-customer-service/?do==add Customer Service forum]. Make sure you provide '''your account username''', '''your server''', the '''version of Python you're using''', and '''the module(s) you need''' including any relevant '''version numbers''' for them. | ||
= Running Python scripts outside cgi-bin folder = | == Running Python scripts outside cgi-bin folder == | ||
Executing a CGI script outside the | Executing a CGI script outside the 'cgi-bin' folder is possible with some modifications. | ||
= Steps = | == Steps == | ||
* Go to | * Go to '/home/<your username>/httpdocs'. (If you were transferred from the old cPanel, the folder will be called 'public_html') | ||
* Create a file named | * Create a file named '.htaccess' and copy/paste the below in: | ||
<pre> | <pre> | ||
Line 90: | Line 104: | ||
</pre> | </pre> | ||
The first two lines make | 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.helioho.st/index.py'. This also supports other CGI file extensions. | ||
= Further Support = | == Further Support == | ||
If this tutorial has not worked for you, please go back and check all of your steps again to make sure you didn't miss anything. If you can't figure out what is wrong, please post in the [https://helionet.org/index/forum/45-customer-service/?do=add Customer Service forum], making sure to provide your '''username''' and any '''error message(s)''' received. | If this tutorial has not worked for you, please go back and check all of your steps again to make sure you didn't miss anything. If you can't figure out what is wrong, please post in the [https://helionet.org/index/forum/45-customer-service/?do==add Customer Service forum], making sure to provide your '''username''' and any '''error message(s)''' received. |
Latest revision as of 18:43, 17 January 2025
Python
The easiest way to get started with Python on a server is with CGI. The only real difference between CGI and running the script on your home computer is that CGI needs to output a 'Content-Type' header.
We also have tutorials for building with Flask, creating a new Django project, and converting an existing Django app to work on HelioHost.
Python Versions Available
Morty Server
Python Version | Python Path | Python Modules Installed |
---|---|---|
3.9 | #!/usr/bin/python3.9 | View |
3.12 | #!/usr/bin/python3.12 | View |
Tommy Server
Python Version | Python Path | Python Modules Installed |
---|---|---|
3.9 | #!/usr/bin/python3.9 | View |
3.12 | #!/usr/bin/python3.12 | View |
Johnny Server
Python Version | Python Path | Python Modules Installed |
---|---|---|
3.9 | #!/usr/bin/python3.9 | View |
3.12 | #!/usr/bin/python3.12 | View |
Steps
- Open the File Manager in Plesk
- Open the 'httpdocs' webroot directory. (If you were transferred from the old cPanel, the directory will be called 'public_html'.)
- Open the 'cgi-bin' directory
- In the top left click the '+' button and select 'Create File'
- Type 'test.py' as your filename
- Click 'OK' to create the file
- Highlight the 'test.py' file and select 'Change Permissions'
- Add a checkmark to the 'Execute / Search' column for all three rows (Owner / Group / Others)
- Click the 'Save' button
- Make sure 'rwx r-x r-x' ('755') is displayed under the Permissions column
- Highlight 'test.py' and select 'Edit in Text Editor'
- Leave 'UTF-8' as the encoding, set 'Line break type' to 'Convert to Unix style' and copy/paste the below into the large text field:
#!/usr/bin/python3.12 print("Content-Type: text/html\n\n") print("HelioHost rules!")
- Click the 'Save' button
- Highlight the 'test.py' file and select 'Open in Browser' (Or manually navigate to: '<your subdomain>.helioho.st/cgi-bin/test.py')
If everything worked, you should see 'HelioHost rules!' displayed in your browser.
Code Explanation
In the code pasted in, the first line is called the 'shebang'. It tells the server which version of Python to use.
The second line of code 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 you an error. The 'Content-Type' header always has to have the two end lines immediately after it. That's what the two '\n' are.
Troubleshooting
If you don't see the module you need, your script will probably give you an error when you try to import the missing module. You can view your account error logs for details on which module is missing and causing the error.
Request Additional Python Modules
Before requesting additional modules, make sure you check the list of modules already installed on your server.
Please do not request modules that are already installed.
After you have checked the list of modules already installed on your server, if there is another module you'd like to request, please create a post in the Customer Service forum. Make sure you provide your account username, your server, the version of Python you're using, and the module(s) you need including any relevant version numbers for them.
Running Python scripts outside cgi-bin folder
Executing a CGI script outside the 'cgi-bin' folder is possible with some modifications.
Steps
- Go to '/home/<your username>/httpdocs'. (If you were transferred from the old cPanel, the folder will be called 'public_html')
- Create a file named '.htaccess' and copy/paste the below in:
Options +ExecCGI AddHandler cgi-script .py DirectoryIndex 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.helioho.st/index.py'. This also supports other CGI file extensions.
Further Support
If this tutorial has not worked for you, please go back and check all of your steps again to make sure you didn't miss anything. If you can't figure out what is wrong, please post in the Customer Service forum, making sure to provide your username and any error message(s) received.