Pyramid 调试工具栏通过 HTTP 而不是 HTTPS 提供静态内容

rch*_*out 3 python pylons python-3.x pyramid pyramid-debug-toolbar

在我们的测试服务器上,我们使用Pyramid 调试工具栏,但是,它会生成http://指向静态内容(如 CSS 和 JavaScript 文件)的链接,而其余内容则通过 HTTPS 提供。这会导致混合内容警告,并破坏所有功能。有没有办法强制它生成HTTPS链接?

我知道可以在 Chrome 中启用混合内容,并且这可行,但对于整个 QA 团队来说这不是一个可行的解决方案。

Ant*_*hon 5

可能有更好/更简单的方法来实现此目的,但是您可以做一件事来实现此目的,将参数添加_scheme='https'到每次调用request.static_url().

为此,您当然可以编辑pyramid/url.py,但您也可以在项目中执行此操作__init__.py

from pyramid.url import URLMethodsMixin

URLMethodsMixin.static_url_org = URLMethodsMixin.static_url  # backup of original

def https_static_url(self, *args, **kw):
    kw['_scheme'] = 'https'  # add parameter forcing https
    return URLMethodsMixin.static_url_org(self, *args, **kw)  # call backup

URLMethodsMixin.static_url = https_static_url  # replace original with backup
Run Code Online (Sandbox Code Playgroud)

static_url类似作品的参数route_url。从文档中:

请注意,如果 _scheme 作为 https 传递,并且未传递 _port,则假定 _port 值已作为 443 传递。同样,如果 _scheme 作为 http 传递且未传递 _port,则假定已传递 _port 值如 80。要避免此行为,请在传递 _scheme 时始终显式传递 _port。设置 '_scheme' 自动强制使用端口 443