Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Passing Information Using POST Method in Python
The POST method is a more secure and reliable way to pass information to a CGI program compared to GET. Instead of appending data to the URL, POST sends information as a separate message through standard input, making it ideal for sensitive data and large forms.
How POST Method Works
Unlike GET method which appends data to the URL after a ?, POST method:
- Sends data as a separate message body
- Doesn't expose sensitive information in the URL
- Can handle larger amounts of data
- Provides better security for form submissions
Example: CGI Script for POST Method
Below is a CGI script that handles both GET and POST methods using Python's cgi module ?
#!/usr/bin/python
# Import modules for CGI handling
import cgi, cgitb
# Create instance of FieldStorage
form = cgi.FieldStorage()
# Get data from fields
first_name = form.getvalue('first_name')
last_name = form.getvalue('last_name')
print("Content-type:text/html\r\n\r\n")
print("<html>")
print("<head>")
print("<title>Hello - Second CGI Program</title>")
print("</head>")
print("<body>")
print("<h2>Hello %s %s</h2>" % (first_name, last_name))
print("</body>")
print("</html>")
HTML Form for POST Method
Here's the HTML form that uses POST method to submit data to our CGI script ?
<form action = "/cgi-bin/hello_get.py" method = "post"> First Name: <input type = "text" name = "first_name"><br /> Last Name: <input type = "text" name = "last_name" /> <input type = "submit" value = "Submit" /> </form>
Key Differences: GET vs POST
| Aspect | GET Method | POST Method |
|---|---|---|
| Data Location | URL parameters | Message body |
| Security | Less secure (visible in URL) | More secure (hidden) |
| Data Limit | Limited by URL length | No practical limit |
| Caching | Can be cached | Not cached |
Form Output
When you fill out the form with first and last name and click submit, the CGI script processes the POST data and displays a personalized greeting. The form data is sent securely without exposing it in the browser's address bar.
Conclusion
POST method provides a more secure way to handle form data in CGI programming. It keeps sensitive information hidden from URLs and supports larger data transfers, making it the preferred choice for most web forms.
