python - 应用程序引擎 : Request Data

标签 python google-app-engine web webapp2

在我的 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 时,我得到以下结果:

http://localhost:8080/products/table
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/,它打印出来:

http://localhost:8080/

参见the docs for the Request class here .

关于python - 应用程序引擎 : Request Data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8456745/

相关文章:

android - Google OAuth2 - 访问 token 和刷新 token -> invalid_grant/代码已被兑换

python - Sendmail 不适用于本地 GAE 本地开发服务器

java - 为什么我的点击事件在 Selenium 中不起作用?

php - 当我的用户代理不包含类似的东西时,我如何用 php 检测用户使用 ipad

python - mysql 中的同一行(nginx+tornado+mysqldb)

python - 如何使用 Zeep 和 Python 3.7 捕获错误

python - 单元格注释和颜色可以使用 gspread 查询吗?

python - 我可以将 Nose 覆盖输出限制到目录(而不是包)吗?

java - 使用 Blob 获取图像并保存到 Google App Engine

Java,我们如何修剪回车符和换行符?