python - 使用python的CGI表单提交按钮

标签 python cgi

我正在尝试创建一个 CGI 表单,允许用户输入一个词,然后它会将这个词发送到下一页(另一个 CGI)。我知道如何使用 .html 文件来完成它,但是当涉及到使用 python/cgi 来完成它时我迷路了。

这是我需要做的,但它是在 html 中。

<html>
<h1>Please enter a keyword of your choice</h1>
<form action="next.cgi" method="get">
Keyword: <input type="text" keyword="keyword">  <br />
<input type="submit" value="Submit" />
</form>
</html>

有谁知道如何用cgi 创建提交按钮?这是我目前所拥有的。

import cgi
import cgitb
cgitb.enable()


form = cgi.FieldStorage()

keyword = form.getvalue('keyword')

最佳答案

要从 Python cgi 页面显示 html,您需要使用 print 语句。

这是一个使用您的代码的示例。

#!/home/python
import cgi
import cgitb
cgitb.enable()

print 'Content-type: text/html\r\n\r'
print '<html>'
print '<h1>Please enter a keyword of your choice</h1>'
print '<form action="next.cgi" method="get">'
print 'Keyword: <input type="text" name="keyword">  <br />'
print '<input type="submit" value="Submit" />'
print '</form>'
print '</html>'

然后在您的 next.cgi 页面上,您可以获取表单提交的值。像这样的东西:

#!/home/python
import cgi
import cgitb
cgitb.enable()

form = cgi.FieldStorage()

keyword = form.getvalue('keyword')

print 'Content-type: text/html\r\n\r'
print '<html>'
print keyword
print '</html>'

关于python - 使用python的CGI表单提交按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13814241/

相关文章:

python - 有兴趣在维基百科 xml 转储中仅搜索与医学相关的术语

Python 中的curl 等价物,用于从文件中发布数据以供温和的库使用

cgi - 如何写入系统/端口/输入?

eclipse - 调试 perl CGI

c++ - cgicc - cgi.getElements(),哪个方法?得到或邮寄?

python - 在 Flask 中使用主机名而不是 IP 地址

python - 构建一棵树 "backwards"

database - 当浏览器重新加载/返回时,如何防止再次写入数据库?

javascript - 如果 .cgi 已被缓存,IE 从第二次开始不会向 .cgi 发出 GET 请求

python - 从子类获取父私有(private)或 protected 值