Intro :
So why should we be interested in CGI which stands for common gateway interface. Well for instance, try to recall the websites that you have been in the last 1 hour. Now out of those websites, some where static and some were dynamic. The latter would mean that the contents of that website kept changing in real time.
CGI scripting is helpful when you want to generate content from the data residing in a database. This is not only convenient but also cuts a lot of time.
In my previous article, I showed how to run CGI scripts in an apache2 webserver. Here is the link, if you wanna take a look at it
CGIHTTPServer
I hope that you have python installed in your system. Just to be sure.
{% highlight bash %}
tasdik@Acer:$ python –version
Python 2.7.8
tasdik@Acer:$
{% endhighlight %}
We will be making use of a super simple Web server shipped by default with python instead of using a full blown web server software for the sake of understanding.
Now cgi scripts are executable files inside the cgi-bin or htdocs directory which the web server executes. After which the output of the program is captured in the standard output to be displayed back by the server.
It is the cgi-bin directory first where all our executable scripts will reside.
{% highlight bash %}
tasdik@Acer:$ cd cgi_demo/
tasdik@Acer:/cgi_demo$ tree
.
├── cgi-bin
│ └── retrieval.py
└── forms.html
1 directory, 2 files tasdik@Acer:~/cgi_demo$ chmod +x cgi-bin/retrieval.py {% endhighlight %}
**NOTE: ** Don't forget to make your cgi-script Executable
/forms.html
{% highlight html linenos %}
User login
/cgi-bin/retrieval.py
{% highlight bash linenos %} #!/usr/bin/env python3.4
import cgi, cgitb cgitb.enable() ## allows for debugging errors from the cgi scripts in the browser
form = cgi.FieldStorage()
getting the data from the fields
first = form.getvalue(‘username’) last = form.getvalue(‘password’)
print(“Content-type:text/html\r\n\r\n”) print("") print("
User has entered
") print("Firstname : " + first + “”) print("
Lastname : " + last + “
”) print("") print("