在我的 webapp2.RequestHandler 方法中:
我想找出请求者想要获取的 uri。例如,如果用户想要获得“ http://www.mysite.com/products/table ”,我想进入一个变量值“table”(在这种情况下)
当我打印“self.request”时,我看到了 RequestHandler 类的所有值,但我没有设法找出在我的情况下什么是正确的属性。
我相信这个问题对你来说很简单,但我只是 python 和应用程序引擎框架的初学者。
在我的 webapp2.RequestHandler 方法中:
我想找出请求者想要获取的 uri。例如,如果用户想要获得“ http://www.mysite.com/products/table ”,我想进入一个变量值“table”(在这种情况下)
当我打印“self.request”时,我看到了 RequestHandler 类的所有值,但我没有设法找出在我的情况下什么是正确的属性。
我相信这个问题对你来说很简单,但我只是 python 和应用程序引擎框架的初学者。
研究了应该如何处理 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
时,我得到以下结果:
函数的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/
,它打印出:
请参阅此处的课程文档Request
。