Python 中使用 POST 方法傳遞資訊
POST 方法是一種通常更可靠的向 CGI 程式傳遞資訊的方法。它以完全相同的方式打包資訊,但不會在 URL 中的 ?後面作為文字字串傳送,而是作為一個單獨的訊息傳送。此訊息以標準輸入的形式進入 CGI 指令碼。
示例
以下為可以處理 GET 和 POST 方法的相同 hello_get.py 指令碼。
#!/usr/bin/python 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>"
輸出
讓我們再次採用與上述相同的示例,它使用 HTML FORM 和提交按鈕傳遞兩個值。我們使用相同的 CGI 指令碼 hello_get.py 來處理此輸入。
<form action = "/cgi-bin/hello_get.py" method = "post"> First Name: <input type = "text" name = "first_name"><br /> Last Name: <input type = "text" name = "last_name" /> <input type = "submit" value = "Submit" /> </form>
以下是上述表單的實際輸出。輸入名字和姓氏,然後單擊提交按鈕以檢視結果。
廣告