Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ssl 将http重定向到twisted中的https_Ssl_Https_Twisted - Fatal编程技术网

Ssl 将http重定向到twisted中的https

Ssl 将http重定向到twisted中的https,ssl,https,twisted,Ssl,Https,Twisted,我正在使用twisted运行django应用程序。我现在从http转到https。如何在twisted中添加从http到https的重定向?在twisted Web中生成重定向的简单方法是使用资源。使用URL实例化它,并将其放入资源层次结构中。如果呈现,它将返回对该URL的重定向响应: from twisted.web.util import Redirect from twisted.web.resource import Resource from twisted.web.server im

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

在twisted Web中生成重定向的简单方法是使用资源。使用URL实例化它,并将其放入资源层次结构中。如果呈现,它将返回对该URL的重定向响应:

from twisted.web.util import Redirect
from twisted.web.resource import Resource
from twisted.web.server import Site
from twisted.internet import reactor

root = Resource()
root.putChild("foo", Redirect("https://stackoverflow.com/"))

reactor.listenTCP(8080, Site(root))
reactor.run()
这将运行一个服务器,该服务器通过重定向到来响应请求

如果您在HTTPS服务器上托管的WSGI容器中运行Django,那么您可能有如下代码:

from twisted.internet import reactor
from twisted.web.wsgi import WSGIResource
from twisted.web.server import Site
from django import some_wsgi_application_object # Not exactly

root = WSGIResource(reactor, reactor.getThreadPool(), some_wsgi_application_object)
reactor.listenSSL(8443, Site(root), contextFactory)

reactor.run()
您只需将第一个示例中的一些代码添加到第二个示例中,就可以运行一个额外的HTTP服务器来生成所需的重定向:

from twisted.internet import reactor
from twisted.web.wsgi import WSGIResource
from twisted.web.util import Redirect
from twisted.web.server import Site
from django import some_wsgi_application_object # Not exactly

root = WSGIResource(reactor, reactor.getThreadPool(), some_wsgi_application_object)
reactor.listenSSL(8443, Site(root), contextFactory)

old = Redirect("https://localhost:8443/")
reactor.listenTCP(8080, Site(old))

reactor.run()

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

然后,您可以使用
RedirectToScheme(“https”)
,代替您要重定向的HTTP站点的
Site()


注意:如果您要重定向的HTTP位于非标准端口上,您可能需要重写URL请求中的
部分。

请详细说明如何使用Twisted运行Django应用程序。对,但对于HTTPS重定向,您不希望重定向到固定URL,但是指向URL a)用户输入的主机名或地址,b)指向用户输入的服务器上的同一路径。(例如,我会在apache mod_rewrite中使用
RewriteRule^var/www redirect/(*)$https://%{SERVER_NAME}/$1[R,L]
)来完成这项工作)。在twisted中使用重定向资源或其他方式是否可以实现这一点?呃,我的mod_重写示例是针对具有文档根的重定向站点
/var/www Redirect
=)当然<代码>重定向实际上是一个非常简单的类。它可以扩展为从请求中获取主机名并在重定向中使用。您还可以查看
ChildRedirector
来了解如何保留请求路径。这通常是有效的,因为Redirecto使用它的第一个参数(
newURLPath
,此处)并将其插入到一个字符串中
%s
。然而,Twisted的最新版本已改为做其他事情。现在将
URLPath
传递到
重定向到
是一个错误。这种感觉有点像Twisted中的回归,但是
重定向到
之前被记录为使用
str
(现在是
字节
)。所以也有人可能认为这是对API的错误使用。无论如何,要使这项工作正常,您应该先将
newURLPath
粉碎成一个字节字符串,然后再将其传递给
redirectTo
-
bytes(newURLPath)
。感谢您的有用编写。我改变的一件事是对我有效的
if newURLPath.scheme==self.newScheme
。你能解释为什么它们不相等意味着重定向循环吗?@AmaJayJB是的,这似乎是个错误。我已经做出了改变。不幸的是,我已经无法访问我最初为之写这篇文章的项目,所以我无法查看它的历史。绝对没有问题。我觉得我遗漏了什么(谢谢你的回复!)
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