【问题标题】:Regex match of hexdigest in google app engine webapp WSGIApplicationgoogle app engine webapp WSGIApplication中hexdigest的正则表达式匹配
【发布时间】:2010-01-05 08:05:40
【问题描述】:
application = webapp.WSGIApplication(
    [(r'/main/profile/([a-f0-9]{40})', ProfileHandler)],
    debug=True)

上述参数中的正则表达式在 Google App Engine 中无法识别 40 十六进制长的十六进制摘要。

我得到的是 404,而不是 ProfileHandler 被传递了匹配的 40 十六进制长配置文件 ID。我的 app.yaml 将所有内容 /main/.* 传递给正确的 python 脚本,所以这不是问题。正则表达式看起来很理智并且resembles the example regex in GAE docs。这个正则表达式有什么问题?

【问题讨论】:

    标签: python regex google-app-engine web-applications


    【解决方案1】:

    我无法重现您的问题。这是我的确切代码:

    index.py

    ​​>
    from google.appengine.ext import webapp
    from google.appengine.ext.webapp.util import run_wsgi_app
    
    class ProfileHandler(webapp.RequestHandler): 
        def get(self, *ar, **kw):
            self.response.out.write("PROFILE IS:" + ar[0])
    
    run_wsgi_app(webapp.WSGIApplication(
    [(r'/main/profile/([a-f0-9]{40})', ProfileHandler),],
                                     debug=True))
    

    app.yaml

    application: someapp
    version: 1
    runtime: python
    api_version: 1
    
    handlers:
    - url: /main/.*
      script: index.py
    

    应用程序正在侦听端口 8082

    GET: http://localhost:8082/main/profile/4c4f630aef49c0065c22eb3dd35a00f5787f4816
    RESPONSE: PROFILE IS:4c4f630aef49c0065c22eb3dd35a00f5787f4816
    

    【讨论】:

    • 我尝试使用 ar 而不是仅使用 ar 作为参数,但起初并没有奏效。然后我将它改回纯 ar,尝试将其上传到 GAE 并得到一个错误,即 get() 恰好需要 2 个参数。将 ar 改回 ar 后,正则表达式匹配并加载了页面。最终结果:hex​​digest 匹配现在可以正常工作。感谢您和 Bart K 的帮助!
    【解决方案2】:

    我没有使用 Google App Engine 的经验,但是:

    • 如果将([a-f0-9]{40}) 更改为([a-fA-F0-9]{40}) 会发生什么情况
    • 您确定使用的是组$1,而不是整个匹配项(包括/main/profile/)?

    【讨论】:

    • 没有这样的运气将大写字母添加到组合中。我不能确定是否要回答您的第二次询问,因为我在 /main/profile/ 末尾拍打的任何 40 十六进制都会将我带到一个完全没有信息的 404 页面。 $1 组应该作为参数传递给 ProfileHandler。如果它到了那么远,我可以判断它是匹配 $1 还是整个 uri
    • 太糟糕了。您将不得不等待或熟悉 Google App Engine 的人。祝你好运!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-10
    • 1970-01-01
    • 2017-12-20
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多