Passing Information Using POST Method in Python

PythonServer Side ProgrammingProgramming

A generally more reliable method of passing information to a CGI program is the POST method. This packages the information in exactly 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. This message comes into the CGI script in the form of the standard input.


Below is same script which handles GET as well as POST method.

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


Let us take again same example as above which passes two values using HTML FORM and submit button. We use same CGI script to handle this input.

<form action = "/cgi-bin/" method = "post">
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:30:46