web.py中获取当前url路径

web.ctx.homepath + web.ctx.fullpath

如果只使用web.ctx.fullpath的话,在有subapplications的应用中就不对了,在subapplications中web.ctx.fullpath是相对于当前的subapplications的,不是全部的路径

看看web.py的源代码就清楚了,下面是web.ctx的初始化代码:

ctx.homedomain = ctx.protocol + '://' + env.get('HTTP_HOST', '[unknown]')
ctx.homepath = os.environ.get('REAL_SCRIPT_NAME', env.get('SCRIPT_NAME', ''))
ctx.home = ctx.homedomain + ctx.homepath
ctx.realhome = ctx.home
ctx.ip = env.get('REMOTE_ADDR')
ctx.method = env.get('REQUEST_METHOD')
ctx.path = env.get('PATH_INFO')
# http://trac.lighttpd.net/trac/ticket/406 requires:
if env.get('SERVER_SOFTWARE', '').startswith('lighttpd/'):
    ctx.path = lstrips(env.get('REQUEST_URI').split('?')[0], ctx.homepath)
    # Apache and CherryPy webservers unquote the url but lighttpd doesn't. 
    # unquote explicitly for lighttpd to make ctx.path uniform across all servers.
    ctx.path = urllib.unquote(ctx.path)

if env.get('QUERY_STRING'):
    ctx.query = '?' + env.get('QUERY_STRING', '')
else:
    ctx.query = ''

ctx.fullpath = ctx.path + ctx.query

对于subapplications有下面的代码:

def _delegate_sub_application(self, dir, app):
    """Deletes request to sub application `app` rooted at the directory `dir`.
    The home, homepath, path and fullpath values in web.ctx are updated to mimic request
    to the subapp and are restored after it is handled. 

    Any issues with when used with yield?
    """
    web.ctx._oldctx = web.storage(web.ctx)
    web.ctx.home += dir
    web.ctx.homepath += dir
    web.ctx.path = web.ctx.path[len(dir):]
    web.ctx.fullpath = web.ctx.fullpath[len(dir):]
    return app.handle_with_processors()
This entry was posted in Python and tagged , . Bookmark the permalink.

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>