【问题标题】:Error 405 when filling form python填写表单python时出现错误405
【发布时间】:2014-07-14 18:33:27
【问题描述】:

我使用 Google 应用引擎编写了一个简单的 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)

如果我使用 urrlib2 而不是请求,也会出现同样的问题。我什至尝试在不发送标头的情况下使用它。

【问题讨论】:

    标签: python html google-app-engine


    【解决方案1】:

    您正在向/ URL 发送POST 请求,但处理程序确实实现了post 方法(因此出现405 Method not allowed 错误)。尝试将请求发送到正确的 URL,即/post

    【讨论】:

    • 好的,我这样做了,它给出了响应 200,但我传递的值作为 JSON 没有被传递。所以在名称应该是的输出中它只是空白。我什至尝试将传递的值存储在数据存储中,但没有存储任何内容。
    • 问题是request.get() 只适用于application/x-www-form-urlencoded 请求。要从 JSON 请求中获取数据,您需要先对其进行解析——参见例如。 stackoverflow.com/a/19677652/1159088
    猜你喜欢
    • 2020-05-30
    • 1970-01-01
    • 1970-01-01
    • 2021-09-05
    • 2014-01-10
    • 2020-10-20
    • 1970-01-01
    • 1970-01-01
    • 2019-09-05
    相关资源
    最近更新 更多