Skip to main content

Python CGI Programming

Python Programming language is broadly used by developers to create standalone, desktop, enterprise and web applications these days. Here in this article, I’m going to cover trending CGI programming in python. If you have just marked your presence in the python programming universe, then I believe you should go for Simplilearn’s Python Training accessible anytime over the internet. Here, you’ll learn about developing simple to complex python based applications with briefly described basics to advanced terms used in programming. It will also make you develop the python programming based game- flappy bird clone.

Python CGI Programming

CGI (Common Gateway Interface), is a group of standards for defining the data exchange among server and customized scripts. The CGI specifications are managed by NCSA.

What is Common Gateway Interface?

CGI is a set of standard designed for external gateway programs. It assists them to connect through the servers such as HTTP server. The latest version available is CGI 1.1 and CGI 1.2 is under development.

Web Browsing

Allow me to explain the concept of CGI in an elaborative way. Let’s check out what happens when you click on a particular hyperlink for browsing a URL:

  • The browser connects with HTTP web server by sending a request demanding for the URL, that is, filename.
  • Web server parses the URL and searches for the name of the file. If it recognise that file, sends it back to the main client such as browser. And, if not, an error message will be indicated, saying you requested a wrong file.
  • Browser collects the response from the web server. It either displays the error message or collected file to the client.

Though, it is easy to create an HTTP server so that server can directly return output to the browser made request by executing the file instead of sending file. This feature is known as Common Gateway Interface or CGI. The programs written for it are known as CGI scripts. If Python is not your primary programming language, don’t worry, you can write CGI programs in PERL, C, C++, Shell etc.

Web Server Configuration

Talking more about the CGI programming always ensure that your web server supports CGI. Also, it is capable of handling CGI scripts. A pre-configured directory will be stored in the HTTP server for running all the CGI scripts. The name of this directory is CGI directory. The convention path will be /var/www/cgi-bin. CGI files support .cgi extension. Best part is you can also keep your python .py extension files in this folder.

The Linux server executes these scripts in cgi-bin directory as /var/www. For defining any specific directory to execute your script, you can make following changes in httpd.conf file:

<Directory "/usr/var/www/cgi-bin">
AllowOverride None
Options ExecCGI +MultiViews
Require all granted
</Directory>

<Directory "/var/www/cgi-bin">
Options All
</Directory>

Now, you got a web server and you are free to execute any program written in C, Perl etc.

Writing CGI Program

Let’s start by writing a simple python script which will be pointing to a CGI script known as firstpgm.py. This file is stored in the folder /var/www/cgi-bin. Now, before execution, make sure to change permission by using Unix command chmod for execution.

Chmod 755 firstpgm.py

File:

#!/usr/bin/python

print "content-type: text/html/r/n/r/n" 
print '<html>'
print '<head>'
print '<title> Hi all- first program</title>'
print '</head>'
print '<body>'
print '<h3> Yo- successfully executed my program</h3>'
print '</body>'
print '</html>'

On execution, an output will be shown:

Yo- successfully executed my program

HTTP Header

In the above program the content-type: text/html\r\n\r\n represents a HTTP header. It is sent to the browser for recognising content.

Various HTTP-headers are: Content-type, Location: URL, Set Cookie: String, Content-Length: N, Last-Modified: Date, Expires: Date.

Environment Variables in CGI

The environment variables are accessible from programs. Few environment variables broadly used in the programming are following:

  • CONTENT_TYPE: It represents the data type of the content sent by client to the server.
  • HTTP_COOKIE: key and value pair cookie setup.
  • PATH_INFO: represents the CGI script path.
  • REMOTE_ADDR: used for authentication. It displays the IP   of the remote host.
  • REQUEST_METHOD: methods used for requests like GET and POST.
  • REMOTE_HOST: It displays the hostname.
  • SCRIPT_NAME: It displays the script name.
  • SERVER_NAME: hostname of server.
  • SERVER_SOFTWARE: name and version of software running over the server.
  • QUERY_STRING: It takes the URL information. This information is taken by GET method.

You can also check the list of environment variables by writing script:

#!/usr/bin/python

import os

print "Content-type: text/html\r\n\r\n";
print "<font size=+4>Environment</font><\br>";
for param in os.environ.keys():
   print "<b>%20s</b>: %s<\br>" % (param, os.environ[param])

Final Words

In this article, I’ve covered the key pillar of the backend programing-CGI. I hope now you’ll able to develop your application in an effective manner. Feel free to ask any question..!!

The post Python CGI Programming appeared first on The Crazy Programmer.



from The Crazy Programmer https://www.thecrazyprogrammer.com/2018/09/python-cgi-programming.html

Comments

Post a Comment

Popular posts from this blog

dotnet sdk list and dotnet sdk latest

Can someone make .NET Core better with a simple global command? Fanie Reynders did and he did it in a simple and elegant way. I'm envious, in fact, because I spec'ed this exact thing out in a meeting a few months ago but I could have just done it like he did and I would have used fewer keystrokes! Last year when .NET Core was just getting started, there was a "DNVM" helper command that you could use to simplify dealing with multiple versions of the .NET SDK on one machine. Later, rather than 'switching global SDK versions,' switching was simplified to be handled on a folder by folder basis. That meant that if you had a project in a folder with no global.json that pinned the SDK version, your project would use the latest installed version. If you liked, you could create a global.json file and pin your project's folder to a specific version. Great, but I would constantly have to google to remember the format for the global.json file, and I'd constan

R vs Python for Machine Learning

There are so many things to learn before to choose which language is good for Machine Learning. We will discuss each and everything about R as well as Python and the situation or problem in which situation we have to use which language. Let’s start Python and R are the two most Commonly used Programming Languages for Machine Learning and because of the popularity of both the languages Novice or you can say fresher are getting confused, whether they should choose R or Python language to commence their career in the Machine learning domain. Don’t worry guys through this article we will discuss R vs Python for Machine Learning. So, without exaggerating this article let’s get started. We will start it from the very Basics things or definitions. R vs Python for Machine Learning Introduction R is a programming language made by statisticians and data miners for statistical analysis and graphics supported by R foundation for statistical computing. R also provides high-quality graphics and

Top Tips For PCB Design Layout

Are you thinking about designing a printed circuit board? PCBs are quite complicated, and you need to make sure that the layout that you choose is going to operate as well as you want it to. For this reason, we have put together some top tips for PCB design layout. Keep reading if you would like to find out more about this. Leave Enough Space One of the most important design tips for PCB layout is that you need to make sure that you are leaving enough space between the components. While many people might think that packing components closely is the best route to take, this can cause problems further down the line. This is why we suggest leaving extra space for the wires that will spread. This way, you’ll have the perfect PCB design layout. Print Out Your Layout Struggling to find out if your components sizes match? Our next tip is to print out your layout and compare the printed version to your actual components. Datasheets can sometimes come with errors, so it doesn’t hurt to do