Python CGI
Receive data
Sample program:
The program is same for both get and post method.#!/usr/bin/python #coding: utf-8 import cgi import os message = '' method = '' method = os.environ['REQUEST_METHOD'] form = cgi.FieldStorage() if( form.has_key('message') ): message = form['message'].value print 'Content-Type: text/html' print print ''' <html> <head> <META http-equiv="Content-Type" content="text/html;charset=utf-8"> <title>Python message test</title> </head> <body> Your message is:<br> %s <br> <br> Method is %s <br> </body> </html> ''' % ( message, method )html for get method
<html> <head> <META http-equiv="Content-Type" content="text/html;charset=utf-8"> <title>Python get test</title> </head> <body> <form method="get" action="message.cgi"> Your message:<br> <input name="message" size="64" /> <input type="submit" value="Submit" /> </form> </body> </html>html for post method
<html> <head> <META http-equiv="Content-Type" content="text/html;charset=utf-8"> <title>Python post test</title> </head> <body> <form method="post" action="message.cgi"> Your message:<br> <input name="message" size="64" /> <input type="submit" value="Submit" /> </form> </body> </html>Try get method
Try post method
Download samples
Description:
#!/usr/local/bin/python #coding: utf-8Put the path of python. For some servers, it should be modified to /usr/local/bin/python or other path.
Declear that character code is utf-8.
method = os.environ['REQUEST_METHOD']Retrive method type.
form = cgi.FieldStorage() if( form.has_key('message') ): message = form['message'].valuecgi.FieldStorage can get data.
print 'Content-Type: text/html' printIt is required to output html by CGI.