如何将文本区域数据传递给Python CGI脚本?
将文本区域数据传递到CGI程序
当必须将多行文本传递给CGI程序时,将使用TEXTAREA元素。
这是带有TEXTAREA框的表单的示例HTML代码-
<form action = "/cgi-bin/textarea.py" method = "post" target = "_blank"> <textarea name = "textcontent" cols = "40" rows = "4"> Type your text here... </textarea> <input type = "submit" value = "Submit" /> </form>
该代码的结果是以下形式-
Type your text here... Submit
以下是textarea.cgi脚本,用于处理Web浏览器给出的输入-
#!/usr/bin/python # Import modules for CGI handling import cgi, cgitb # Create instance of FieldStorage form = cgi.FieldStorage() # Get data from fields if form.getvalue('textcontent'): text_content = form.getvalue('textcontent') else: text_content = "Not entered" print "Content-type:text/html\r\n\r\n" print "<html>" print "<head>"; print "<title>Text Area - Fifth CGI Program</title>" print "</head>" print "<body>" print "<h2> Entered Text Content is %s</h2>" % text_content print "</body>"