ssl - 将 http 重定向到扭曲的 https

标签 ssl https twisted

我正在使用 twisted 运行一个 django 应用程序。我现在从 http 转到 https。如何在扭曲中添加从 http 到 https 的重定向?

最佳答案

从 HTTP 上的任何给定路径重定向到 HTTPS 上的相同路径 (基于 Jean-Paul 针对我的评论提出的建议):

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 部分。

关于ssl - 将 http 重定向到扭曲的 https,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5311229/

相关文章:

c# - 以编程方式完成时,http 添加 sslcert 失败

api - GAE API 资源管理器不显示 API,似乎卡在加载中

php - 使用安全 session cookie 在 HTTP 和 HTTPS 页面之间切换

python - Twisted 如何与命令行程序交互? Node.js 可以吗?

Python - Twisted 客户端 - 检查 ping 循环中的 protocol.transport 连接

ssl - 安装 .cer 证书 debian

spring-mvc - Spring RestTemplate 不允许持久的 https 连接

php - Zend 1.12 显示 Twitter 提要

python - HTTPS Python 客户端

python - 处理来自同一事件循环的 SimpleXMLRPCServer 调用和 Asyncore.dispatchers 事件