Perl and Python Scripts
Python and Perl are interpreter-based, high-level, and general-purpose scripting languages. Both are used to set a system and its interfaces together.
What are the differences between Perl and Python?
While Perl was initially created to simplify the process of making reports, Python is used to simplify the code-writing process using logical and simple code. Both Perl and Python are used for small- and large-scale applications and projects.
What are the file extensions for Perl and Python?
Perl
.pl
Python
.py
Both of these scripts must have 755 permissions.
Perl and Python script examples
Perl Script
#!/usr/bin/perl -w
print "Content-type: text/html\n\n";
print "<html><head>";
print "<title>CGI Test</title>";
print "</head><body>";
print "<p>Test page using Perl</p>";
print "</body></html>";
Python Script
#!/usr/bin/python
print "Content-type: text/html\n\n";
print "<html><head>";
print "<title>CGI Test</title>";
print "</head><body>";
print "<p>Test page using Python</p>";
print "</body></html>";
Downloading Python
Download and extract the latest Python version (3.12.2) to your server by running the command below. Or you can go to https://www.python.org/downloads/.
mkdir ~/python
cd ~/python
wget https://www.python.org/ftp/python/3.12.2/Python-3.12.2.tgz
tar zxfv Python-3.10.5.tgz
find ~/python -type d | xargs chmod 0755
cd Python-3.12.2
Installing Python
Run the following commands to configure and install Python once Python has been extracted to your server.
./configure --prefix=$HOME/python
make
make install
Activating Python
Please note that you have to add a line in the .htaccess file to use Python.
The recommended directive is:
Addhandler cgi-script .py .pl .cgi
(This directive covers Perl, Python, and CGI files.)
Modifying the .bashrc
In order for Python's local version to load, add the following codes to the .bashrc file:
vim ~/.bashrc
Press i.
Enter:
export PATH=$HOME/python/Python-3.12.2/:$PATH
Write the changes by pressing ESC and then close the vim:
:wq
Press Enter.
source ~/.bashrc
Getting a 500 Internal Server error?
If you are getting a 500 Internal Server Error, it is most likely caused by the lack of executable permissions. Check to make sure your Perl or Python script file has permissions of 755.