在我的 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)
当我访问 URL http://localhost:8080/products/table
时,我得到以下结果:
get
函数的resource
参数由WSGIApplication
自动传入url_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/
,它打印出来:
关于python - 应用程序引擎 : Request Data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8456745/