1

在我的 webapp2.RequestHandler 方法中:

我想找出请求者想要获取的 uri。例如,如果用户想要获得“ http://www.mysite.com/products/table ”,我想进入一个变量值“table”(在这种情况下)

当我打印“self.request”时,我看到了 RequestHandler 类的所有值,但我没有设法找出在我的情况下什么是正确的属性。

我相信这个问题对你来说很简单,但我只是 python 和应用程序引擎框架的初学者。

4

1 回答 1

3

研究了应该如何处理 URL 和通配符 URL。尝试这个:

class ProductsHandler(webapp.RequestHandler):
    def get(self, resource):
        self.response.headers['Content-Type'] = 'text/plain'
        table = self.request.url
        self.response.out.write(table)
        self.response.out.write("\n")
        self.response.out.write(resource)

def main():
    application = webapp.WSGIApplication([
        ('/products/(.*)', ProductsHandler)
        ],
        debug=True)
    util.run_wsgi_app(application)

当我转到 URLhttp://localhost:8080/products/table时,我得到以下结果:

http://localhost:8080/products/table

函数的resource参数由get自动传入,因为它映射到:WSGIApplication url_mapping

('/products/(.*)', ProductsHandler)

(.*)是一个通配符,并作为方法参数传入。

您可以将get方法中的参数命名为任何您想要的名称,而不是 . resource,例如table. 但这并没有多大意义,因为如果你传入一个类似的 url http://localhost:8080/products/fish,它将不再包含“table”这个词。


早期尝试(编辑前):

尝试这样的事情:

class MainHandler(webapp.RequestHandler):
    def get(self):
        table = self.request.url
        self.response.out.write(table)

对于我的测试,我去了http://localhost:8080/,它打印出:

http://localhost:8080/

请参阅此处的课程文档Request

于 2011-12-10T14:13:07.907 回答