How to run Python scripts
- Category : cPanel Question
- Posted on : Nov 17, 2021
- Views : 1,384
- By : HostSEO
If you wish to run Python scripts in your hosting account, you can create and edit them in two ways: either in cPanel or via SSH.
To create and edit Python script in cPanel use the following steps:
1. Log in to your cPanel:
2. Go to section Files > File Manager:
3. You can choose whether you want to run the script in the cgi-bin folder or outside of it.
The cgi-bin folder contains executable CGI scripts. If the file should be placed out of the cgi-bin folder, .htaccess should be created additionally.
4. In order to run the Python script in the cgi-bin folder follow the steps:
- Go to the cgi-bin folder
- Create a file with a .py extension, for example nctest.py in the /home/cPuser/public_html/cgi-bin directory (where cPuser is your actual cPanel username).
5. In order to create the file, click New File > specify the name of the file with the required extension and click Create New File:
6. Change the permission of the file to 0755
NOTE: Files are created with default permissions 0644. The .py file will become executable when the permissions are changed to 0755.
7. In order to change the permissions of the file you need to select the file > click Change Permissions > Mark Execute for User, Group, World columns and click Change Permissions:
8. Open the file, click Edit and add the code.
We will add the following script for testing purposes:
#!/usr/bin/python
print "Content-type:text/html\r\n\r\n"
print "How to run Python scripts in cPanel"
NOTE: The file should start with the path to the Python scripts that is /usr/bin/python on our servers, but you can run the whereis python command via SSH to check the directory.
Now the script should work using http://domain.com/cgi-bin/nctest.py
If you would like to run the Python file not in the cgi-bin folder (in public_html or any other directory), it is necessary to add the following code to the .htaccess file in the same directory where the Python script is placed:
Options +ExecCGI
AddHandler cgi-script .py
To create and edit the Python script via SSH use the following steps:
1. Connect to cPanel account via SSH
2. Go to thte cgi-bin directory running cd ./public_html/cgi-bin
To check that you are in the correct directory, you can run the command: pwd
3. Create the file with the .py extension, for example, nctest.py in the /home/cPuser/public_html/cgi-bin directory (where cPuser is your cPanel username).
To create the file in the current directory, type touch nctest.py
Once done, you can check that the file is present in the directory now running the command ls
4. Change the permission of the file to 0755 running the command below:
chmod 755 /home/cPuser/public_html/cgi-bin/nctest.py
(where cPuser is your actual cPanel username)
5. Open the file and add the necessary code.
You can open the file with one of the editors like nano. To open the file with nano, use the following command:
nano nctest.py
We will add the following code for testing purposes:
#!/usr/bin/python
print "Content-type:text/html\r\n\r\n"
print "How to run Python scripts in cPanel"
NOTE: The file should start with the path to the Python scripts that is /usr/bin/python on our servers, but you can run the whereis python command via SSH to check the directory.
To save the changes, click Crtl+O and press Enter for Windows or Command+O for Mac OS.
Now the script should work using http://domain.com/cgi-bin/nctest.py
If you would like to run the Python script not in the cgi-bin folder, it is necessary to add a special code to the .htaccess file in the same directory where the Python file is placed.
6. Create .htaccess if it is not present in the required directory running touch .htaccess
7. Open the .htaccess file running nano .htaccess and add the following code:
Options +ExecCGI
AddHandler cgi-script .py
Click Crtl+O and press Enter for Windows or Command+O for Mac OS to save the changes:
Now you can check the script at http://domain.com/nctest.py
That's it!
Categories
- cPanel Question 47
- cPanel Software Management 29
- cPanel Tutorials 13
- Development 29
- Domain 13
- General 19
- Linux Helpline (Easy Guide) 156
- Marketing 47
- MySQL Question 13
- News 2
- PHP Configuration 14
- SEO 4
- SEO 42
- Server Administration 84
- SSL Installation 54
- Tips and Tricks 24
- VPS 3
- Web Hosting 44
- Website Security 22
- WHM questions 13
- WordPress 148
Subscribe Now
10,000 successful online businessmen like to have our content directly delivered to their inbox. Subscribe to our newsletter!Archive Calendar
Sat | Sun | Mon | Tue | Wed | Thu | Fri |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 | 31 |
Recent Articles
-
Posted on : Sep 17
-
Posted on : Sep 10
-
Posted on : Aug 04
-
Posted on : Apr 01
Tags
- ts
- myisam
- vpn
- sql
- process
- kill
- tweak
- server load
- attack
- ddos mitigation
- Knowledge
- layer 7
- ddos
- webmail
- DMARC
- Development
- nginx
- seo vpn
- Hosting Security
- wireguard
- innodb
- exim
- smtp relay
- smtp
- VPS Hosting
- cpulimit
- Plesk
- Comparison
- cpu
- encryption
- WHM
- xampp
- sysstat
- optimize
- cheap vpn
- php-fpm
- mariadb
- apache
- Small Business
- Error
- Networking
- VPS
- SSD Hosting
- Link Building
- centos
- DNS
- optimization
- ubuntu