Python script showing code itself in browser instead of running!!
Python is a widely used general-purpose, high-level programming language.
Its design philosophy emphasizes code readability, and its syntax allows programmers to express concepts in fewer lines of code than would be possible in languages such as C++ or Java.
There are some points that you’ve to check before starting-up with your Python script on cPanel server. Here I’m listing some quick points that you should remember before going with Python script.
Hello world!! – A sample Python script to test this!!
Yep, to test this you must have a test Python code. Here is the hello world test code:
#! /usr/bin/python print "Content-type: text/html\n\n" print "Hello world!!"
In cPanel, by-default the Python is located under “/usr/bin/python“. If there was any change, you can check the location by running the following command:
Create the file with extension “.py” to load the Python script!!
If you have shell access you can create it by using your favorite file editor (vi, nano etc). You can use the filemanager feature, if you have only the cPanel access. And then add the test code into the file. Please change the file ownership to ‘user.user’, after creating it as root user.
chown user.user test.py
user :: Replace with cPanel username.
test.py :: The test file you created.
It should have executable permission!!
Yes, the file must has executable permission, so that it can execute the script.
chmod +x test.py
Still I’m in the middle of “Python script showing code itself in browser instead of running”. Ohh, then your apache conf missing the AddHandler for Python.
Don’t forget to add AddHandler in .htacces file!!
Options +ExecCGI AddHandler cgi-script .py