Enabling cgi-script in webserver to read python files

Hiteshkoolwal
2 min readAug 6, 2020

--

Today, i will tell you how to enable cgi scripting in apache webserver in redhat pachage.

First we have to open httpd configuration file and add few lines.
path: vim /etc/httpd/conf/httpd.conf

Here in <Directory “/var/www/cgi-bin”>
add following lines
Options +ExecCGI
AddHandler cgi-script .cgi .py .pi

Here +ExecCGI in options tells the tells Apache to execute CGI files which are uploaded to the /var/www/cgi-bin directory.
The AddHandler tells Apache that any file ending in .cgi, .pl, or .py is considered a CGI script.

Now, we have to restart https webserver
systemctl restart httpd

Then give executable permission to file.
chmod +x sample.py
now file appears in green color.

Make a small cgi python3 code to check everything worked perfectly.

#/usr/bin/python3
import cgi
import subprocess
print("content-type: text/html")
print()
mydata = cgi.Fieldstorage()
myx = mydata.getvalue("x") # x is input value from api
print("Input cmd is : ", myx)
output = subprocess.getoutput("sudo " + myx)
print("Output is : ", output)

The sample.py file executed perfectly

  • Blog Summary
    - Edit httpd.conf file
    - restart httpd
    - Create sample.py file
    - chmod +x sample.py
    - curl localhost/cgi-bin/sample.py

Thank you

--

--