我正在使用twisted 运行一个django 应用程序。我现在从 http 迁移到 https。如何在twisted 中添加从http 到https 的重定向?
从 HTTP 上的任何给定路径重定向到 HTTPS 上的同一路径
(基于让-保罗对我的评论的建议):
from twisted.python import urlpath
from twisted.web import resource, util
class RedirectToScheme(resource.Resource):
"""
I redirect to the same path at a given URL scheme
@param newScheme: scheme to redirect to (e.g. https)
"""
isLeaf = 0
def __init__(self, newScheme):
resource.Resource.__init__(self)
self.newScheme = newScheme
def render(self, request):
newURLPath = request.URLPath()
# TODO Double check that == gives the correct behaviour here
if newURLPath.scheme == self.newScheme:
raise ValueError("Redirect loop: we're trying to redirect to the same URL scheme in the request")
newURLPath.scheme = self.newScheme
return util.redirectTo(newURLPath, request)
def getChild(self, name, request):
return self
然后你可以使用RedirectToScheme("https")
,代替你的Site()
对于您要重定向的 HTTP 站点。
注意:如果您要重定向的 HTTP 位于非标准端口上,您可能会遇到:<port>
您还需要重写 URLRequest 中的部分。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)