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