在twisted中将http重定向到https
我正在使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从 HTTP 上的任何给定路径重定向到 HTTPS 上的同一路径
(基于 Jean-Paul 针对我的评论的建议):
然后您可以使用
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):
Then you can use
RedirectToScheme("https")
, in place of yourSite()
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.在 Twisted Web 中生成重定向的一种简单方法是使用 Redirect 资源。使用 URL 实例化它并将其放入资源层次结构中。如果它被渲染,它将返回到该 URL 的重定向响应:
这将运行一个服务器来响应 http:// /localhost:8080/ 并重定向到 https://stackoverflow.com/。
如果您在 HTTPS 服务器上托管的 WSGI 容器中运行 Django,那么您的代码可能如下所示:
您可以运行一个额外的 HTTP 服务器,只需添加第一个服务器中的一些代码即可生成您想要的重定向第二个例子的例子:
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:
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:
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: