What is the difference between GET and POST in Python CGI Programming?

In Python CGI Programming, the two primary methods for passing information from a web browser to a web server are GET and POST. Understanding their differences is crucial for web development security and functionality.

GET Method

The GET method sends encoded user information appended to the page request. The page and the encoded information are separated by the ? character as follows −

http://www.test.com/cgi-bin/hello.py?key1=value1&key2=value2

The GET method is the default method to pass information from browser to web server. It produces a long string that appears in your browser's Location box. Never use GET method if you have password or other sensitive information to pass to the server.

GET Method Limitations

  • Size limitation: only 1024 characters can be sent in a request string
  • Data is visible in the URL
  • Not suitable for sensitive information
  • Information is accessible through QUERY_STRING environment variable

Simple URL Example − GET Method

Here is a simple URL that passes two values to a CGI program using GET method ?

/cgi-bin/hello_get.py?first_name=ZARA&last_name=ALI

Below is the hello_get.py script to handle input given by web browser using the cgi module ?

#!/usr/bin/python3
# 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 - CGI Program</title>")
print("</head>")
print("<body>")
print("<h2>Hello %s %s</h2>" % (first_name, last_name))
print("</body>")
print("</html>")

This would generate the following result ?

Hello ZARA ALI

HTML Form Example − GET Method

This example passes two values using HTML FORM and submit button ?

<form action = "/cgi-bin/hello_get.py" method = "get">
    First Name: <input type = "text" name = "first_name"><br />
    Last Name: <input type = "text" name = "last_name" />
    <input type = "submit" value = "Submit" />
</form>

POST Method

The POST method is generally more reliable for passing information to a CGI program. It packages the information in the same way as GET methods, but instead of sending it as a text string after a ? in the URL, it sends it as a separate message through standard input.

POST Method Advantages

  • No size limitations like GET method
  • Data is not visible in the URL
  • Suitable for sensitive information like passwords
  • Data comes through standard input, not query string

CGI Script for POST Method

The same hello_get.py script handles both GET and POST methods seamlessly ?

#!/usr/bin/python3
# Import modules for CGI handling
import cgi, cgitb

# Create instance of FieldStorage
form = cgi.FieldStorage()

# Get data from fields (works for both GET and POST)
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 - CGI Program</title>")
print("</head>")
print("<body>")
print("<h2>Hello %s %s</h2>" % (first_name, last_name))
print("</body>")
print("</html>")

HTML Form Example − POST Method

The same form structure, but using POST method ?

<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>

Comparison

Feature GET Method POST Method
Data Location URL query string HTTP request body
Size Limit 1024 characters No practical limit
Security Data visible in URL Data hidden from URL
Bookmarkable Yes No
Best Use Search queries, navigation Form submissions, sensitive data

Conclusion

Use GET method for simple data retrieval and navigation where data visibility is not a concern. Use POST method for form submissions, sensitive data, or when dealing with large amounts of data. The cgi.FieldStorage() class handles both methods seamlessly in Python CGI programming.

Updated on: 2026-03-24T19:59:49+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements