填写表单时出现405错误 Python

0 投票
1 回答
1652 浏览
提问于 2025-04-18 13:17

我用谷歌应用引擎写了一个简单的WSGI服务器。下面是它的代码:

    import webapp2

    class MainHandler(webapp2.RequestHandler):
        def get(self):
            self.response.write('''
                    <html><head><title>BLAH</title></head>
                    <body>
                    <form type = "input" action = "/post" method = post>
                    <input type = text name = "name">
                    <input type = submit value = "Submit">
                    </form>
                    <hr>
                    </body>
                    </html>
                    ''')

    class EntrySubmission(webapp2.RequestHandler):
        def post(self):
            n = self.request.get('name')
            self.response.write('''<html><head><title>%s</title></head>
                        <body>
                        <h1>Welcome %s</h1>
                        <br>How are you today?
                        <br><a href = "/">Back</a>
                        <hr>
                        </body>
                        </html>''' % (n,n))

    app = webapp2.WSGIApplication([
        ('/', MainHandler),
        ('/post',EntrySubmission),
    ], debug=True)

这个服务器在我手动输入值的时候运行得很好。但是当我用Python输入值时,它却返回了405的响应。这可能是什么问题呢?这是我用Python输入的代码:

    import requests,json
    url = 'http://localhost:9080/'
    header = {'content-type' : 'application/json'}
    d = {'name' : 'Foo'}
    r = requests.post(url,data = json.dumps(d),headers = header)

如果我用urllib2代替requests,问题还是一样。我甚至试过不发送头信息。

1 个回答

0

你正在向 / 这个网址发送一个 POST 请求,但处理这个请求的程序并没有实现 post 方法,所以你会看到 405 Method not allowed 的错误。试着把请求发送到正确的网址,也就是 /post

撰写回答