CGI stands for Common Gateway Interface
It define a standard that define how the communication of information should be between the web server and a written script.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
+--------------------------------------+ | | | Web Server (e.g. Apache) | | | +--------------------------------------+ | | HTTP Request v +--------------------------------------+ | | | Python CGI Script (e.g. .py file) | | | +--------------------------------------+ | | HTTP Response v +--------------------------------------+ | | | Web Browser | | | +--------------------------------------+ |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# Import the cgi module to handle incoming data import cgi # Define a list to store information about cars cars = [] # Get the incoming data from the form form = cgi.FieldStorage() # Check if the form has been submitted if "make" in form: # Get the make and model from the form data make = form.getvalue("make") model = form.getvalue("model") # Add the new car information to the list cars.append({"make": make, "model": model}) # Generate the HTML response print("Content-Type: text/html") print() print("<html>") print("<head>") print("<title>Cars</title>") print("</head>") print("<body>") print("<h1>Cars</h1>") # Generate a table to display the cars print("<table>") print("<tr>") print("<th>Make</th>") print("<th>Model</th>") print("</tr>") for car in cars: print("<tr>") print(f"<td>{car['make']}</td>") print(f"<td>{car['model']}</td>") print("</tr>") print("</table>") # Generate the form to add new cars print("<h2>Add a Car</h2>") print("<form action='car.py' method='post'>") print("<label for='make'>Make:</label>") print("<input type='text' id='make' name='make'>") print("<br>") print("<label for='model'>Model:</label>") print("<input type='text' id='model' name='model'>") print("<br>") print("<input type='submit' value='Add'>") print("</form>") print("</body>") print("</html>") |
- A client, such as a web browser, sends an HTTP request to the web server.
- The web server routes the request to a Python CGI script, which is a program that is written in Python and executed on the server.
- The CGI script retrieves data, performs calculations, and generates an HTTP response, which is sent back to the web server.
- The web server returns the HTTP response to the client, which is displayed in the web browser.
This is a high-level overview of the basic architecture of a Python CGI application. In reality, the process is more complex, with many additional components and interactions, but this diagram provides a good starting point for understanding how a Python CGI application works.
Below is an example of Python CGI to build a simple application that allows users to add and view information about cars:
This code will handle incoming data from a form, add the data to a list of cars, and generate an HTML response that displays the list of cars and a form for adding new cars.
When this script is executed on the server, it will generate an HTML page that can be viewed in a web browser. The form allows users to add new cars by entering the make and model, and the table displays all of the cars that have been added. Each time the form is submitted, the information is added to the list, and the HTML response is updated to reflect the new data.