填写表单python时出现错误405

时间:2014-07-14 18:33:27

标签: python html google-app-engine

我使用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而不是请求,则会出现同样的问题。我甚至尝试使用它而不发送标题。

1 个答案:

答案 0 :(得分:0)

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