Step 1: configure httpd.conf
<Directory "/Applications/XAMPP/xamppfiles/cgi-bin"> AllowOverride None Options None Require all granted </Directory>
<Directory "/Applications/XAMPP/xamppfiles/cgi-bin"> AddHandler cgi-script .cgi .py AllowOverride All Options +Indexes +FollowSymLinks +ExecCGI Order allow,deny Allow from all Require all granted </Directory>
If no “Require all granted”, it will show “Access Forbidden” when you visit your file on browser.
Step 2: put your python file e.g. test.py in the folder cgi-bin
Example is as below:
#!/usr/bin/python print "Content-type: text/html\n" print "Hello, world!"
use sudo chmod 755 test.py to make it executable.
Step 3: then go to browser, type “http://localhost/cgi-bin/test.py”, you will see “Hello World”.
Please don’t forget “Content-type: text/html\n”, otherwise it will show:
The server encountered an internal error and was unable to complete your request.
malformed header from script ‘test.py’: Bad header: Hello, world!
In addition, you may need to change the permission of cgi-bin and test.py.
The commands are: sudo chmod 775 cgi-bin
and: sudo chmod 755 cgi-bin/test.py