[英]How do I accept post requests with a python script using cgi?
I want to submit text to an HTML form and send it to a Python script. 我想将文本提交到HTML表单并将其发送到Python脚本。 When I press the submit button it takes me to an error page that says:
当我按下提交按钮时,它会转到一个错误页面,上面写着:
Error code: 501 Message: Unsupported method ('POST').
错误代码:501消息:不支持的方法('POST')。 Error code explanation: HTTPStatus.NOT_IMPLEMENTED - Server does not support this operation.
错误代码说明:HTTPStatus.NOT_IMPLEMENTED - 服务器不支持此操作。
I'm not exactly sure what the issue is. 我不确定问题是什么。 I've googled the error message and have looked through different documentation and watched some videos but they are seem to gloss over how to set up the server correctly.
我已经搜索了错误消息并查看了不同的文档并观看了一些视频,但它们似乎掩盖了如何正确设置服务器。
I'm running Python 3.7. 我正在运行Python 3.7。
<form action="www/cgi-bin/hello.py" method="post">
First Name:
<input type="text" name="first_name" placeholder="e.g. Barack" required="required"/>
Last Name:
<input type="text" name="last_name" placeholder="e.g. Obama" required="required"/>
<input type="submit" name="submitprompt" value="Submit"/>
</form>
#!/usr/bin/python
import cgi, cgitb
first_name = form.getvalue('first_name')
last_name = form.getvalue('last_name')
print("Content-type:text/html")
print("")
print("")
print("Hello - Second CGI Program")
print("")
print("")
print(" Hello %s %s" % (first_name, last_name))
print("")
print("")
I'm expecting to be taken a page that has the text: Hello first_name last_name
. 我希望得到一个包含文本的页面:
Hello first_name last_name
。 Instead, I receive the aforementioned error. 相反,我收到上述错误。
使用命令“python3 -m http.server --cgi 8000”并从那里配置解决了我的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.