Artificial Intelligence
Please fill the required field.

Design a basic CGI script using a programming language (e.g., Python, Perl) to capture user input from a form and display the result. Explain the script's key functions.


Below is a simple example of a CGI (Common Gateway Interface) script written in Python. This script captures user input from an HTML form and then displays the result back to the user. ### Basic CGI Script Example (Python) 1. **Create the HTML Form**: Before implementing the CGI script, we need an HTML form to collect user input. Save this HTML code as `form.html`. ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>User Input Form</title> </head> <body> <h1>Please Enter Your Name</h1> <form action="/cgi-bin/display.py" method="post"> <label for="name">Name:</label> <input type="text" id="name" name="name" required> <input type="submit" value="Submit"> </form> </body> </html> ``` 2. **Create the CGI Script**: Now create a file named `display.py` in the `/cgi-bin/` directory of your web server. Here’s the CGI script: ```python #!/usr/bin/env python3 import cgi import cgitb # Enable debugging cgitb.enable() print("Content-Type: text/html") # HTTP Header print() # Blank line indicates end of headers # Create instance of FieldStorage form = cgi.FieldStorage() # Retrieve the input value from the form name = form.getvalue('name') # Display the result print(f''' <html> <head> <title>User Input Result</title> </head> <body> <h1>Your name is: {name}</h1> </body> </html> ''') ``` ### Key Functions Explained: 1. **Shebang Line (`#!/usr/bin/env python3`)**: - This line indicates that the script should be run using the Python 3 interpreter. 2. **Import Statements**: - `import cgi`: The `cgi` module provides functionality to handle form data. - `import cgitb`: This module helps in debugging CGI scripts by providing detailed error messages in the browser. 3. **Print HTTP Headers**: - The line `print("Content-Type: text/html")` specifies that the content being returned is HTML. - The following `print()` statement outputs a blank line, which is needed to indicate the end of headers. 4. **FieldStorage Instance**: - `form = cgi.FieldStorage()`: This creates an instance of `FieldStorage`, which is used to parse form data submitted in either GET or POST methods. 5. **Getting Form Data**: - `name = form.getvalue('name')`: This retrieves the value of the input field with the name `name` that was submitted in the form. 6. **Generating HTML Output**: - The script concludes with an HTML block that displays the user's input. The variable `name` is inserted into the HTML using an f-string. ### Running the Script: 1. Place the `form.html` file in the directory accessible by your web server. 2. Configure your web server to use the CGI directory (usually `/cgi-bin/`). 3. Access the `form.html` file through your web browser, enter your name, and submit the form. 4. The `display.py` script will be executed, and the result will be returned to the browser showing the name entered by the user. ### Note: Make sure your web server has CGI enabled, and the script file (`display.py`) has permission to be executed (using `chmod +x display.py` in a UNIX-like system).