嗨我有一個應用程序運行扭曲。我希望它通過https而不是http運行。我在哪裏可以找到一個很好的例子?扭曲在https
Q
扭曲在https
3
A
回答
5
您只需使用reactor.listenSSL
而不是reactor.listenTCP
。 http://twistedmatrix.com/documents/current/core/howto/ssl.html涵蓋reactor.listenSSL
的基礎知識。
1
這裏是基本認證SSL上運行的扭曲服務器的一個示例:HTTPS是SSL上的HTTP
#!/usr/bin/env python
USERS={'admin': 'admin',
'user': 'user',
'test': 'eW91IGFyZSBjcmF6eQo='}
"""
Twisted SSL webserver with basic authentication using plain in-memory passwords.
The first argument is the path of the directory to serve; if not provided then the current folder is used (".").
INSTALL DEPENDENCIES:
pip install twisted
pip install pyOpenSSL
pip install service_identity
GENERATE SSL CERTIFICATES:
mkdir ~/.ssl && cd ~/.ssl
openssl genrsa > privkey.pem
openssl req -new -x509 -key privkey.pem -out cacert.pem -days 9999
USAGE:
Requires running as root (normal users cannot bind to ports below 1024);
login with test_user/test_password
sudo python twisted-web-ssl.py # serve the current folder
sudo python twisted-web-ssl.py /home
"""
import os
import sys
from twisted.web.static import File
from zope.interface import implements
from twisted.python import log
from twisted.internet import reactor, ssl
from twisted.web import server, resource, guard
from twisted.cred.portal import IRealm, Portal
from twisted.cred.checkers import InMemoryUsernamePasswordDatabaseDontUse
from twisted.python.log import startLogging
startLogging(sys.stdout)
home_dir = os.path.expanduser("~")
sslContext = ssl.DefaultOpenSSLContextFactory(
os.path.join(home_dir, '.ssl/privkey.pem'),
os.path.join(home_dir, '.ssl/cacert.pem'),
)
class SimpleRealm(object):
implements(IRealm)
def __init__(self, path):
self.path = path
def requestAvatar(self, avatarId, mind, *interfaces):
if resource.IResource in interfaces:
return resource.IResource, File(self.path), lambda: None
raise NotImplementedError()
def main(root):
log.startLogging(sys.stdout)
checkers = [InMemoryUsernamePasswordDatabaseDontUse(**USERS)]
wrapper = guard.HTTPAuthSessionWrapper(
Portal(SimpleRealm(root), checkers),
[guard.DigestCredentialFactory('md5', 'whatever.com')])
reactor.listenSSL(443, server.Site(resource=wrapper),
contextFactory=sslContext)
reactor.run()
if __name__ == '__main__':
root = sys.argv[1] if len(sys.argv) > 1 else '.'
main(root)
0
。 HTTPS = HTTP + SSL/TLS。見文件http://twistedmatrix.com/documents/13.0.0/core/howto/ssl.html#auto1和http://twistedmatrix.com/documents/13.0.0/web/howto/using-twistedweb.html#auto2
這是一個簡單的例子:
from twisted.internet import reactor, ssl
from twisted.web import server, resource
sslContext = ssl.DefaultOpenSSLContextFactory(
'/Users/wucao/Desktop/https/2_gw2.vsgames.cn.key', # Private Key
'/Users/wucao/Desktop/https/1_gw2.vsgames.cn_bundle.crt', # Certificate
)
class MainResource(resource.Resource):
isLeaf = True
def render_GET(self, request):
request.responseHeaders.addRawHeader("Content-Type", "text/html; charset=utf-8")
return "<html><body>Hello World</body></html>"
site = server.Site(MainResource())
reactor.listenSSL(443, site, sslContext)
reactor.run()
運行和測試https://localhost
相關問題
- 1. HTTPS請求扭曲
- 2. 扭曲的HTTPS客戶端
- 3. HTTPS請求扭曲,檢查證書
- 4. 將http重定向到https以扭曲
- 5. 扭曲和扭曲Movieclip
- 6. 在扭曲
- 7. 扭曲和ULIMIT
- 8. ImageMagick扭曲
- 9. 扭曲效果
- 10. 學習扭曲
- 11. 背景扭曲
- 12. 使用扭曲
- 13. JTable autoresize,扭曲?
- 14. 扭曲字體
- 15. CATiledlayer扭曲CGPath
- 16. python3.5扭曲?
- 17. ZMQ和扭曲
- 18. AbstractFactory與扭曲
- 19. 扭曲和符號鏈接在扭曲/ python/lockfile.py
- 20. 扭曲的Python在Eclipse上
- 21. 根據病情在扭曲
- 22. 在Raphael扭曲「路徑」
- 23. 扭曲在centos缺少mail.smtp?
- 24. 使用DeferredQueue在扭曲
- 25. 扭曲在Mac問題
- 26. 模式在扭曲的Python
- 27. 扭曲。在spawnProcess的processExited
- 28. 扭曲的adbapi:runInteraction last_insert_id()
- 29. 問題與扭曲
- 30. VTK圖像扭曲?