Whenever we use a hyperlink in our websites and click on that hyperlink to visit a particular website or a URL, the browser interacts with the Hypertext transfer protocol (HTTP) web. The web server then uses a URL and searches a similar filename. If the match is found, then the file is sent back. If the file is not found in the second case, an error message is displayed that we have entered the wrong file name.
A web browser is used in taking a reply from the web server and shows the content. This can set the server of HTTP so that whenever we request a specific file, then this file must be executed instead of sending it back. Whatever is the content of that file is executed as a program and is sent back to the browser to display the file. For python, these codes are declared as Python scripts.
If you are willing to execute Python scripts on the web, you have to study how to execute python as a CGI script.
Apache webserver configuration for CGI
Configuration of the server is needed to get our CGI scripts. This will be done by setting a directory as Script Alias Directive. You may use other options by downloading “xamp” on your system. The “scriptAlias” is used to map between “filesystem” paths and the URLs. The script alias uses a way of having the effect of making the target directory as that it contains only the CGI scripts. Setting a directory as “scriptAlias” Directive is essential because it recognizes that all the files that exist in the directory are CGI scripts. Usually, a script Alias looks like the below link in “HTTP.conf” of the apache web server.
Run CGI from a specific directory
To prepare a specific directory to display the CGI script, you need to use the following link.
<Directory /usr/local/apache2/htdocs/dir(any directory)>Options +ExecCGI</Directory>
Where ‘dir’ is the directory of your own choice, by using the above-mentioned configuration, you need to specify the following to tell the extensions of the server of CGI files you are willing to run.
# AddHandler cgi-script .cgi .pl
This piece of information plays an important role in telling Apache to execute files with .CGI and .PL extensions as CGI.
IIS web server for CGI configuration
The operating system we are up to is Windows 10. That is similar to the windows server 2007 and 2008 as well. Whatever the operating system is, you must have a running python interpreter on the Windows Operating system.
For this purpose, go to the taskbar. Open ‘control panel’. Now click on the ‘Programs’. Select the desired option of the windows feature. A small window is opened having the title of ‘windows features’. Navigate to the Internet ‘Internet’s information services’. Now go to the ‘application Development Feature’ and move down to the check box of CGI. To continue, select ‘ok’.
Now go to the start and then move towards the IIS manager window. Here go to the ‘Default website’ on the left panel. Right-click ‘default website’ and then click the ‘Add application’.
Now in the ‘Alias’ text box, enter the name. i.e., “PythonApp”. And in the ‘physical path’ add the path where the “Pythonapp” application is saved. Then click “ok” to proceed further.
After the configuration of the webserver to run python as CGI, we now try to run a simple code on the webserver. Some points should be followed before starting.
- Make sure that the Python code doesn’t contain any syntax errors.
- Make sure your file has, read, write, executed for administrator privileges so that it can be modified wherever you are going to run this file, either on Windows, Linux, or Unix.
Execute First sample file
Use HTML code for the input fields.
We have shared a small piece of the code of form-making only. This coding is to create a text file and a button for the submission of data. You have to write the coding of all essentials of HTML. We have entered the name that will appear on the python when we run the code.
Now moving towards the python code.
# form = cgi.FieldStorage()
# name = form.getvalue(‘fname’)
# print(“Name of the user is:”,name)
The output will be:
Now let us explain the above written Python code. Firstly, we need to import the basic modules for the execution.
“Cgitb.enable()” helps you when nothing good is happening. Or you find any error. But one thing that should be mentioned here is that it does not guide you if you encounter EOF issues or any sort of syntax error. It has some other purposes.
# form = cgi.FieldStorage()
Now we have used text files of the CGI module in the form. The name will be extracted by HTML form using:
A print command is used to display the text we have entered in the textbox on the HTML page. This command will take the data from the getvalue(‘fname’) function. A variable is used here to store the fetched value.
CGI programming advantages
There are many useful aspects of CGI programming. Some of them are mentioned here.
- These programs are language-independent.
- The CGI programs can work on any web server.
- They are portable and can increase the dynamic communication between web applications.
Some of the disadvantages also need some attention.
- These programs are hard to debug as they are too complex.
- The processing time required is very long.
- Mostly the data is not stored in the cache memory.
‘python CGI example’ is an article of a practically creating connection between the server and the client. This is done by undergoing various steps and procedures both at the server and client sides. We have used a simple example to print a name on Python when it has been entered into the browser. An additional feature of HTML is added for creating the input sample page. I hope our intentions of making you aware of CGI usage will work.