Passing Information using GET method in Python

PythonServer Side ProgrammingProgramming

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

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 program using GET method.

Below is script to handle input given by web browser. We are going to use cgi module, which makes it very easy to access passed information −


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


This would generate the following result −


Simple FORM Example:GET Method

This example passes two values using HTML FORM and submit button. We use same CGI script to handle this input.

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


Here is the actual output of the above form, you enter First and Last Name and then click submit button to see the result.

Published on 31-Jan-2020 12:29:46