简体   繁体   中英

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. When I press the submit button it takes me to an error page that says:

Error code: 501 Message: Unsupported method ('POST'). Error code explanation: HTTPStatus.NOT_IMPLEMENTED - Server does not support this operation.

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.

<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 . Instead, I receive the aforementioned error.

使用命令“python3 -m http.server --cgi 8000”并从那里配置解决了我的问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM