Computer >> Máy Tính >  >> Lập trình >> Python

Làm cách nào để chuyển Dữ liệu vùng văn bản sang tập lệnh Python CGI?

Chuyển Dữ liệu Vùng Văn bản sang Chương trình CGI

Phần tử TEXTAREA được sử dụng khi văn bản nhiều dòng phải được chuyển đến Chương trình CGI.

Đây là mã HTML mẫu cho biểu mẫu có hộp TEXTAREA -

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

Kết quả của mã này là dạng sau -

Type your text here...
 Submit

Dưới đây là tập lệnh textarea.cgi để xử lý đầu vào do trình duyệt web cung cấp -

#!/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>"