【问题标题】:app-engine: Request Data应用引擎:请求数据
【发布时间】:2012-01-17 09:50:07
【问题描述】:

在我的 webapp2.RequestHandler 方法中:

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

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

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

【问题讨论】:

    标签: python google-app-engine web webapp2


    【解决方案1】:

    研究了应该如何处理 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)
    

    当我转到 URL http://localhost:8080/products/table 时,我得到了这个结果:

    http://localhost:8080/products/table

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

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

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

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


    早期尝试(编辑前):

    试试这样的:

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

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

    http://localhost:8080/

    the docs for the Request class here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      • 2015-08-04
      • 2017-04-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多