将 html 表单值发布到 python 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15965646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Posting html form values to python script
提问by Vittal Cherala
I have created html form with text box and button
我创建了带有文本框和按钮的 html 表单
enter ur search keyword
输入您的搜索关键字
My requirement is to pass the text box value in to my test.py code, is there any way to do it. Please suggest me how do it.
我的要求是将文本框值传递给我的 test.py 代码,有什么办法可以做到。请建议我怎么做。
采纳答案by scottydelta
in the form action form action="", put the location of your cgi script and the value of the textbox will be passed to the cgi script.
eg.
在表单 action 中form action="",放置 cgi 脚本的位置,文本框的值将传递给 cgi 脚本。例如。
<form name="search" action="/cgi-bin/test.py" method="get">
Search: <input type="text" name="searchbox">
<input type="submit" value="Submit">
</form>
in your test.py
在你的 test.py
import cgi
form = cgi.FieldStorage()
searchterm = form.getvalue('searchbox')
thus you will get the key word entered in search text box in searchterm variable in python.
因此,您将在 python 中的 searchterm 变量的搜索文本框中获得输入的关键字。
回答by Xanthoula Atsalaki
I had the problem with getting the code in browser. Installed python and xampp. I put the .py script in cgi-bin and I called it from an html page like that
我在浏览器中获取代码时遇到了问题。安装了python和xampp。我将 .py 脚本放在 cgi-bin 中,然后从这样的 html 页面调用它
<form name="input" action="../cgi-bin/name_of_script.py" method="get">
Put the html page in htdocs of xampp. In the httd.conf find the line
将html页面放在xampp的htdocs中。在 httd.conf 中找到该行
AddHandler cgi-script .cgi .pl .asp
and change it to
并将其更改为
AddHandler cgi-script .cgi .pl .asp .py
In the script add the version of the python you have for example I added
在脚本中添加你所拥有的 python 版本,例如我添加的
#!C:\Python27\python.exe
because I have installed python27 in the above directory.
因为我在上面的目录中安装了python27。
Also if you print something in python put this line on top of the script
此外,如果您在 python 中打印某些内容,请将这一行放在脚本的顶部
print "Content-Type: text/html; charset=utf-8\n\n";
The above script of the searchbox it worked fine for me. My operating system is Windows really torchered me, I searched and searched for the above solution.
上面的搜索框脚本对我来说很好用。我的操作系统是 Windows 真的让我很生气,我搜索并搜索了上述解决方案。

