CGI Programming in Python - GeeksforGeeks

08 Dec, 2020
What is CGI?Common Gateway Interface (also known as CGI) is not a kind of language but just a specification(set of rules) that helps to establish a dynamic interaction between a web application and the browser (or the client application). The CGI programs make possible communication between client and web servers. Whenever the client browser sends a request to the webserver the CGI programs send the output back to the web server based on the input provided by the client-server.
CGI is the standard for programs to interface with HTTP servers.CGI programming is written dynamically generating webpages that respond to user input or webpages that interact with software on the server
CGI is the standard for programs to interface with HTTP servers.
CGI programming is written dynamically generating webpages that respond to user input or webpages that interact with software on the server
Working of CGIWhen a request is made by the client-server to the webserver, the CGI uses external script files to handle such requests. These files could be written in any language. The main objective of these script files is to retrieve the data from the database quickly and more efficiently. These scripts convert the retrieved data into an Html format that sends the data to these web servers in Html formatted page.
Install apache2 on your system can we will run ‘hello.py’ on host ‘127.0.0.1’It is recommended to have basic knowledge of HTML before trying this example.hello.py
#!/usr/bin/python3# Importing the 'cgi' moduleimport cgi    print("Content-type: text/html\r\n\r\n")print("<html><body>")print("<h1> Hello Program! </h1>")# Using the inbuilt methods  form = cgi.FieldStorage()if form.getvalue("name"):    name = form.getvalue("name")    print("<h1>Hello" +name+"! Thanks for using my script!</h1><br />")if form.getvalue("happy"):    print("<p> Yayy! I'm happy too! </p>")if form.getvalue("sad"):    print("<p> Oh no! Why are you sad? </p>")  # Using HTML input and forms methodprint("<form method='post' action='hello2.py'>")print("<p>Name: <input type='text' name='name' /></p>")print("<input type='checkbox' name='happy' /> Happy")print("<input type='checkbox' name='sad' /> Sad")print("<input type='submit' value='Submit' />")print("</form")print("</body></html>")
Some advantages of CGI are discussed as below:1 They are portable and can work on almost any web server and operating system.2 They are language-independent.3 They are quite scalable programs in nature which means they can perform both simple and complex tasks.4 CGIs enhance dynamic communication in web applications.5They also aid in making businesses more profitable by decreasing development and maintenance costs.
Some disadvantages of CGI are as below:1 The interpreter has to evaluate a CGI script every time the program is initiated this creates a lot of traffic as there are too many requests from the side of the client-server.2 Programming and designing of CGI programs is very complex and ambiguous.3 CGI programs may compromise server security as most of them are free and easily available making them quite vulnerable.
This article is contributed by Harsh Wardhan Chaudhary (Intern). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
nidhi_biet
gaurisharma360
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Python Dictionary
Read a file line by line in Python
How to Install PIP on Windows ?
Different ways to create Pandas Dataframe
Python String | replace()
Create a Pandas DataFrame from Lists
Python program to convert a list to string
Selecting rows in pandas DataFrame based on conditions
Reading and Writing to text files in Python
sum() function in Python