【问题标题】:Unsupported method POST, by simple python server不支持的方法 POST,通过简单的 python 服务器
【发布时间】:2016-08-03 11:19:48
【问题描述】:

我正在学习 Udacity 的全栈开发,我们必须使用 http.server 创建一个简单的 python 服务器。

我在 python 中编写了一个小程序,它可以很好地执行 GET 请求。 我的帖子有问题。 服务器在本地主机的 8080 端口上运行。我给出的任何 POST 请求都返回 501 不受支持的方法错误。 我主要关注内核设备驱动程序等,我不习惯调试这样的错误。

该程序正在创建一个简单的服务器,该服务器在 GET 请求上打印问候消息。 http:localhost:8080/hello 它还为用户提供了一个输入新问候消息的表单,但输入时会出现 501 错误。 POST 方法应该显示与用户输入的问候语相同的页面。我使用 CGI 来完成这项工作。

我被困住了! 另外,如果有人可以提供有关如何调试此类事情的链接/提示,我将不胜感激!有没有我可以读取的日志文件之类的?

程序:

from http.server import BaseHTTPRequestHandler, HTTPServer
import cgi


class WebServerHandler(BaseHTTPRequestHandler):

    def do_GET(self):
        if self.path.endswith("/hello"):
            self.send_response(200)
            self.send_header('Content-type', 'text/html')
            self.end_headers()
            output = ""
            output += "<html><body>"
            output += "<h2> How's it going?</h2>"
            output += "<form method = 'POST' enctype='multipart/form-data' action='/hello'> What would you like me to say?</h2><input name = 'message' type = 'text'> <input type = 'submit' value = 'Submit'></form>"
            output += "</body></html>"
            self.wfile.write(output.encode(encoding='utf_8'))
            print (output)
            return
        if self.path.endswith("/hola"):
            self.send_response(200)
            self.send_header('Content-type', 'text/html')
            self.end_headers()
            output = ""
            output += "<html><body>&#161hola  <a href = /hello> Back to English! </a>"
            output += "<form method = 'POST' enctype='multipart/form-data' action='/hello'> What would you like me to say?</h2><input name = 'message' type = 'text'> <input type = 'submit' value = 'Submit'></form>"
            output += "</body></html>"
            self.wfile.write(output.encode(encoding='utf_8'))
            print (output)
            return

        else:
            self.send_error(404, 'File Not Found:')


def do_POST(self):
        try:
            self.send_response(200)
            self.send_header('Content-type', 'text/html')
            self.end_headers()
            ctype, pdict = cgi.parse_header(
                self.headers.getheader('content-type'))
            if ctype == 'multipart/form-data':
                fields = cgi.parse_multipart(self.rfile, pdict)
                messagecontent = fields.get('message')
            output = ""
            output += "<html><body>"
            output += " <h2> Okay, how about this: </h2>"
            output += "<h1> %s </h1>" % messagecontent[0]
            output += '''<form method='POST' enctype='multipart/form-data' action='/hello'><h2>What would you like me to say?</h2><input name="message" type="text" ><input type="submit" value="Submit"> </form>'''
            output += "</body></html>"
            self.wfile.write(output.encode(encoding = "utf_8"))
            print (output)
        except:
            pass




def main():
    try:
        port = 8080
        server = HTTPServer(('', port), WebServerHandler)
        print ("Web Server running on port: 8080")
        server.serve_forever()
    except KeyboardInterrupt:
        print (" ^C entered, stopping web server....")
        server.socket.close()

if __name__ == '__main__':
    main()

【问题讨论】:

  • docs.python.org/3/library/… 正如您在该链接中看到的那样,您的班级不支持 do_POST。我相信这可能是错误。尝试 POST 到非 CGI url 时输出错误 501,“只能 POST 到 CGI 脚本”
  • 谢谢!我一直在尝试修复它,但我不知道从哪里开始。任何帮助将不胜感激:D
  • 我对那台服务器了解不多,我以前用的是cherrypy,很好用,好用,如果你想设置一个小型服务器Flask或Bottle非常适合。这些是 python 的 web 框架

标签: python http server


【解决方案1】:

你的do_POST 函数没有正确缩进,它现在在类之外,让它与do_GET 函数对齐应该没问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-19
    • 2020-04-13
    • 2019-12-17
    • 1970-01-01
    • 2023-02-05
    • 1970-01-01
    相关资源
    最近更新 更多