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

Truyền dữ liệu vùng văn bản sang chương trình CGI bằng Python

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.

Ví dụ

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

Truyền dữ liệu vùng văn bản sang chương trình CGI bằng Python

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