我使用的是扭曲的django應用程序。我現在從http移動到https。如何在http中添加重定向到https?將http重定向到https以扭曲
回答
在Twisted Web中生成重定向的簡單方法是使用Redirect資源。使用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()
正確,但對於HTTPS重定向,您希望不重定向到固定URL,而是重定向到URL a)用戶輸入的任何主機名或地址,以及b)用戶輸入的服務器上的相同路徑。 (例如,我會在apache mod_rewrite中使用'RewriteRule^var/www-redirect /(.*)$ https://%{SERVER_NAME}/$ 1 [R,L]')來執行此操作。這是可能的重定向資源或其他方式扭曲? – rakslice 2011-05-19 04:05:28
呃,我的mod_rewrite例子是用於文檔根目錄'/ var/www-redirect'的重定向站點。 =) – rakslice 2011-05-19 04:11:52
當然。 'Redirect'實際上是一個非常簡單的類,
從任何給定的路徑上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()
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位於非標準端口上,那麼URLRequest中可能還有一個:<port>
部分,您還需要重寫該部分。
這用於工作,因爲redirectTo用於取其第一個參數(此處爲'newURLPath')並將其插入到具有'%s'的字符串中。然而,更新版本的Twisted已經改變爲做其他事情。現在將一個'URLPath'傳遞給'redirectTo'是一個錯誤。這種感覺就像Twisted中的一種迴歸 - 但'redirectTo'被記錄爲在之前(現在是'bytes')採用'str'。所以......人們也可以說這是對API的錯誤使用。無論如何,爲了實現這個目標,你應該在將'newURLPath'傳遞給'redirectTo'''bytes(newURLPath)'之前將它砸成一個字節字符串。 – 2016-11-21 14:38:03
- 1. 將HTTPS重定向到HTTP
- 2. 將http重定向到https(https將http重定向到默認值)
- 3. 將http重定向到https並將www重定向到非www
- 4. 重定向到HTTPS HTTP只
- 5. https到http重定向
- 6. apache http:重定向到https:
- 7. 重定向從http://到https://
- 8. HTTP到HTTPS重定向
- 9. HTTPS到HTTP重定向
- 10. HTTP到HTTPS重定向
- 11. 重定向HTTP到HTTPS
- 12. Sinatra + HTTPS重定向到HTTP?
- 13. HTTPS重定向到HTTP
- 14. HTTP到HTTPS重定向
- 15. HTTPS到HTTP重定向nginx
- 16. 重定向http頁到https
- 17. HAProxy將http重定向到https(ssl)
- 18. 將網頁從HTTP重定向到HTTPS
- 19. 通過.htaccess將http重定向到https
- 20. 使用Apache將HTTP重定向到HTTPS
- 21. Docker nginx將HTTP重定向到HTTPS
- 22. 如何將http重定向到https
- 23. 如何將http重定向到https?
- 24. IIS 7.0將https重定向到http
- 25. 將HTTP重定向到HTTPS Htaccess
- 26. 問題將HTTP重定向到https
- 27. 將NGINX http重定向到https
- 28. nginx將HTTPS重定向到HTTP
- 29. .htaccess將https url重定向到http
- 30. 如何將HTTPS重定向到HTTP?
請詳細說明您如何使用Twisted運行您的Django應用程序。 – MattH 2011-03-15 15:45:07