Setting up Python CGI scripts with Apache2 is simple. It is great to add HTTP support to any script easily.
First of all, make sure Apache and Python(3)is installed:
$ apt-get install apache2 python3
In newer versions Python CGI is supported out of the box.
Create a file called cgi.py with the following dummy content. As you can see the cgi lib is imported, and we use the cgi.FieldStorage method to get GET-values printed.
#!/usr/bin/python3 # -*- coding: UTF-8 -*- import cgi import cgitb; cgitb.enable() print('Content-Type: text/html') print('') arguments = cgi.FieldStorage() for i in arguments.keys(): print(arguments[i].value)
In your web root directory, create a directory. I called mine host-cgi/. Create a file in your Apache configuration, and Add Option +ExecCGI, and the Handler cgi-script .py pointing to the newly created directory.
<Directory /var/www/html/host-cgi> Options +ExecCGI AddHandler cgi-script .py </Directory>
$ service apache2 restart
Now, go and call your HTTP CGI script (host: localhost, directory: host-cgi, script: cgi.py, GET parameters: add what you want):
Now you should be able to see the GET variable "servername" printed, that is "hostname01". You can add all the GET parameters you want to the URL, and it will get printed.
Now it is just to go and implement this model with any Python library or script you know, and you can add HTTP support in a very few steps, tho i will recommend you to create a more extensive class handling the whole CGI procedure, but depends on the script it is a wrapper for.
Hope it is useful.