GET requests should only be used to retrieve simple url instances: GET MethodThe following is a simple URL. Use the GET method to send two parameters to the hello_get.py program:
/cgi-bin/hello_get.py?first_name=ZARA&last_name=ALI
The code for the hello_get.py file is as follows:
#! /Usr/bin/python #-*-coding: UTF-8-*-# CGI processing module import cgi, cgitb # create FieldStorage instantiation form = cgi. fieldStorage () # obtain data first_name = form. getvalue ('first _ name') last_name = form. getvalue ('Last _ name') print "Content-type: text/html \ r \ n" print"
"Browser request output result:
Hello ZARA ALI
Simple Form example: GET MethodThe following is a form in HTML that uses the GET method to send two data to the server. The submitted server script is also a hello_get.py file. The Code is as follows:
Use the POST method to transmit dataUsing the POST method to transmit data to the server is more secure and reliable. For example, some sensitive information such as user passwords must be transmitted using POST.
The following is also hello_get.py, which can also process the POST form data submitted by the browser:
#! /Usr/bin/python #-*-coding: UTF-8-*-# import CGI Module import cgi, cgitb # create FieldStorage instance form = cgi. fieldStorage () # obtain the form data first_name = form. getvalue ('first _ name') last_name = form. getvalue ('Last _ name') print "Content-type: text/html \ r \ n" print"
"The following table submits data to the server script hello_get.py through the POST method:
Passing checkbox data through CGI programCheckbox is used to submit one or more option data. The HTML code is as follows:
The code for the checkbox. cgi file is as follows:
#! /Usr/bin/python #-*-coding: UTF-8-*-# introduce CGI processing module import cgi, cgitb # create FieldStorage instance form = cgi. fieldStorage () # receives field data if form. getvalue ('maths '): math_flag = "ON" else: math_flag = "OFF" if form. getvalue ('physics '): physics_flag = "ON" else: physics_flag = "OFF" print "Content-type: text/html \ r \ n" print"
"Transmit Radio data through CGI programRadio only transmits one data to the server. The HTML code is as follows:
The script code of radiobutton. py is as follows:
#!/usr/bin/python# -*- coding: UTF-8 -*-# Import modules for CGI handling import cgi, cgitb # Create instance of FieldStorage form = cgi.FieldStorage() # Get data from fieldsif form.getvalue('subject'): subject = form.getvalue('subject')else: subject = "Not set"print "Content-type:text/html\r\n\r\n"print "
"Passing Textarea data through CGI programTextarea transmits multiple lines of data to the server. The HTML code is as follows:
The textarea. cgi script code is as follows:
#!/usr/bin/python# -*- coding: UTF-8 -*-# Import modules for CGI handling import cgi, cgitb # Create instance of FieldStorage form = cgi.FieldStorage() # Get data from fieldsif form.getvalue('textcontent'): text_content = form.getvalue('textcontent')else: text_content = "Not entered"print "Content-type:text/html\r\n\r\n"print "
Passing drop-down data through CGI programThe code of the HTML drop-down box is as follows:
The dropdown. py script code is as follows:
#!/usr/bin/python# -*- coding: UTF-8 -*-# Import modules for CGI handling import cgi, cgitb # Create instance of FieldStorage form = cgi.FieldStorage() # Get data from fieldsif form.getvalue('dropdown'): subject = form.getvalue('dropdown')else: subject = "Not entered"print "Content-type:text/html\r\n\r\n"print "
" Use Cookie in CGIA major disadvantage of http protocol is that it does not judge the user identity, which brings great inconvenience to programmers,
The appearance of the cookie function makes up for this defect.
All cookies are used to write record data on the client's hard disk through the client's browser while the client accesses the script. When the client accesses the script the next time, the data information is retrieved to identify the user, cookies are often used in password determination.
Cookie syntaxThe http cookie is sent through the http header, Which is earlier than the file transfer. The header's set-cookie syntax is as follows:
Set-cookie:name=name;expires=date;path=path;domain=domain;secure
- Name = name: the cookie value needs to be set (";" and ",", "cannot be used for name). If there are multiple name values, separate them with"; ", for example, name1 = name1; name2 = name2; name3 = name3.
- Expires = date: cookie validity period. Format: expires = "Wdy, DD-Mon-yyyy hh: MM: SS"
-
- Path = path: set the path supported by the cookie. If the path is a path, the cookie takes effect for all files and subdirectories in this directory. For example: path = "/cgi-bin/". If path is a file, the cookie is effective for this file, for example, path = "/cgi-bin/cookie. cgi ".
- Domain = domain: the domain name that takes effect for the cookie, for example: domain = "www.chinalb.com"
- Secure: If this flag is provided, the cookie can only be transmitted through the https server of the SSL protocol.
- Cookie receiving is implemented by setting the environment variable HTTP_COOKIE. The CGI program can retrieve the cookie information by searching the variable. Cookie settings
The Cookie setting is very simple. The cookie will be sent separately in the http header. The following instances set the UserID and Password in the cookie:
#!/usr/bin/python # -*- coding: UTF-8 -*- print "Set-Cookie:UserID=XYZ;\r\n" print "Set-Cookie:Password=XYZ123;\r\n" print "Set-Cookie:Expires=Tuesday, 31-Dec-2007 23:12:40 GMT";\r\n" print "Set-Cookie:Domain=www.w3cschool.cc;\r\n" print "Set-Cookie:Path=/perl;\n" print "Content-type:text/html\r\n\r\n" ...........Rest of the HTML Content....
The preceding example uses the Set-Cookie header information to Set Cookie information. Other attributes of the Cookie, such as the expiration time Expires, Domain name Domain, and Path, are Set in the options. This information is set before "Content-type: text/html \ r \ n.
Retrieve Cookie InformationThe Cookie information retrieval page is very simple. The Cookie information is stored in the CGI Environment Variable HTTP_COOKIE. The storage format is as follows:
key1=value1;key2=value2;key3=value3....
The following is a simple CGI program for retrieving cookie information:
#!/usr/bin/python# -*- coding: UTF-8 -*-# Import modules for CGI handling from os import environimport cgi, cgitbif environ.has_key('HTTP_COOKIE'): for cookie in map(strip, split(environ['HTTP_COOKIE'], ';')): (key, value ) = split(cookie, '='); if key == "UserID": user_id = value if key == "Password": password = valueprint "User ID = %s" % user_idprint "Password = %s" % password
The output result of the above script is as follows:
User ID = XYZPassword = XYZ123
File Upload instance:
To set the form for uploading files in HTML, you must set the enctype attribute to multipart/form-data. The Code is as follows:
The code of the save_file.py script file is as follows:
#! /Usr/bin/python #-*-coding: UTF-8-*-import cgi, osimport cgitb; cgitb. enable () form = cgi. fieldStorage () # obtain the file name fileitem = form ['filename'] # Check whether the file is uploaded if fileitem. filename: # Set the file path fn = OS. path. basename (fileitem. filename) open ('/tmp/' + fn, 'wb '). write (fileitem. file. read () message = 'The file "'+ fn +'" was uploaded successfully 'else: message = 'no file was uploaded' print "" \ Content-Type: text/html \ n
"% (Message ,)If the system you are using is Unix/Linux, you must replace the file separator. In the window, you only need to use the open () Statement:
fn = os.path.basename(fileitem.filename.replace("\\", "/" ))
File Download Dialog BoxCreate the foo.txt file in the current directory for program download.
File Download is implemented by setting the HTTP header information. The function code is as follows:
#! /Usr/bin/python #-*-coding: UTF-8-*-# HTTP header print "Content-Disposition: attachment; filename = \ "foo.txt \" \ r \ n "; # open the file fo = open (" foo.txt "," rb ") str = fo. read (); print str # close the file fo. close ()