在twisted中将http重定向到https

发布于 2024-10-21 18:36:15 字数 88 浏览 1 评论 0原文

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

I'm running a django app using twisted. I moved now from http to https. How can I add redirects from http to https in twisted?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷清清 2024-10-28 18:36:15

从 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") 代替 HTTP 站点的 Site()您想要重定向的位置。

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

To redirect from any given path on HTTP to the same path on the HTTPS
(based on Jean-Paul's suggestions in response to my comment):

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

Then you can use RedirectToScheme("https"), in place of your Site() for the HTTP site that you want to redirect from.

Note: If the HTTP that you want to redirect from is on a non-standard port, you will probably have a :<port> part in the the URLRequest that you'll also need to rewrite.

一个人的旅程 2024-10-28 18:36:15

在 Twisted Web 中生成重定向的一种简单方法是使用 Redirect 资源。使用 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()

这将运行一个服务器来响应 http:// /localhost:8080/ 并重定向到 https://stackoverflow.com/

如果您在 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()

An easy way to generate redirects in Twisted Web is is with the Redirect resource. Instantiate it with a URL and put it into your resource hierarchy. If it is rendered, it will return a redirect response to that 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()

This will run a server which responds to a request for http://localhost:8080/ with a redirect to https://stackoverflow.com/.

If you're running Django in the WSGI container hosted on an HTTPS server, then you might have code that looks something like this:

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()

You can run an additional HTTP server which generates the redirects you want by just adding some of the code from the first example to this second example:

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文