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 GET method in Python
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 and 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.
The GET method has size limitation: only 1024 characters can be sent in a request string. The GET method sends information using QUERY_STRING header and will be accessible in your CGI Program through QUERY_STRING environment variable.
You can pass information by simply concatenating key and value pairs along with any URL or you can use HTML <FORM> tags to pass information using GET method.
Simple URL Example: GET Method
Here is a simple URL, which passes two values to hello_get.py program using GET method ?
http://localhost/cgi-bin/hello_get.py?first_name=John&last_name=Doe
Below is hello_get.py script to handle input given by web browser. We are going to use cgi module, which makes it very easy to access passed information ?
Example
#!/usr/bin/python3
# Import modules for CGI handling
import cgi, cgitb
# Enable CGI error reporting
cgitb.enable()
# 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>")
Output
This would generate the following result ?
Hello John Doe
Simple FORM Example: GET Method
This example passes two values using HTML FORM and submit button. We use same CGI script hello_get.py to handle this input ?
<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>
Key Points
- URL Visibility: GET parameters are visible in the browser URL
- Size Limit: Maximum 1024 characters in the request string
- Security: Not suitable for sensitive data like passwords
- Caching: GET requests can be cached and bookmarked
- Usage: Best for retrieving data and search queries
Conclusion
The GET method is ideal for passing small amounts of non-sensitive data through URL parameters or HTML forms. Use POST method when dealing with sensitive information or large data sets.
