twisted over https
up vote
3
down vote
favorite
Hi I have an app running with twisted. I want it to run over https instead of http. Where can I find a good example for that?
https twisted
add a comment |
up vote
3
down vote
favorite
Hi I have an app running with twisted. I want it to run over https instead of http. Where can I find a good example for that?
https twisted
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
Hi I have an app running with twisted. I want it to run over https instead of http. Where can I find a good example for that?
https twisted
Hi I have an app running with twisted. I want it to run over https instead of http. Where can I find a good example for that?
https twisted
https twisted
asked Mar 6 '11 at 12:41
alexarsh
1,76042739
1,76042739
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
up vote
5
down vote
accepted
All you need to do is use reactor.listenSSL
instead of reactor.listenTCP
. http://twistedmatrix.com/documents/current/core/howto/ssl.html covers the basics of reactor.listenSSL
.
add a comment |
up vote
2
down vote
Here is an example of a twisted server running on SSL with basic authentication:
#!/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 pyOpenSSL 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)
# alternative credential storage implementations: https://twistedmatrix.com/documents/current/api/twisted.cred.checkers.ICredentialsChecker.html
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)
add a comment |
up vote
2
down vote
HTTPS is HTTP over SSL. HTTPS = HTTP + SSL/TLS. See document http://twistedmatrix.com/documents/13.0.0/core/howto/ssl.html#auto1 and http://twistedmatrix.com/documents/13.0.0/web/howto/using-twistedweb.html#auto2
This is the simple example:
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()
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f5210527%2ftwisted-over-https%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
accepted
All you need to do is use reactor.listenSSL
instead of reactor.listenTCP
. http://twistedmatrix.com/documents/current/core/howto/ssl.html covers the basics of reactor.listenSSL
.
add a comment |
up vote
5
down vote
accepted
All you need to do is use reactor.listenSSL
instead of reactor.listenTCP
. http://twistedmatrix.com/documents/current/core/howto/ssl.html covers the basics of reactor.listenSSL
.
add a comment |
up vote
5
down vote
accepted
up vote
5
down vote
accepted
All you need to do is use reactor.listenSSL
instead of reactor.listenTCP
. http://twistedmatrix.com/documents/current/core/howto/ssl.html covers the basics of reactor.listenSSL
.
All you need to do is use reactor.listenSSL
instead of reactor.listenTCP
. http://twistedmatrix.com/documents/current/core/howto/ssl.html covers the basics of reactor.listenSSL
.
answered Mar 6 '11 at 15:31
Jean-Paul Calderone
41.1k570101
41.1k570101
add a comment |
add a comment |
up vote
2
down vote
Here is an example of a twisted server running on SSL with basic authentication:
#!/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 pyOpenSSL 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)
# alternative credential storage implementations: https://twistedmatrix.com/documents/current/api/twisted.cred.checkers.ICredentialsChecker.html
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)
add a comment |
up vote
2
down vote
Here is an example of a twisted server running on SSL with basic authentication:
#!/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 pyOpenSSL 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)
# alternative credential storage implementations: https://twistedmatrix.com/documents/current/api/twisted.cred.checkers.ICredentialsChecker.html
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)
add a comment |
up vote
2
down vote
up vote
2
down vote
Here is an example of a twisted server running on SSL with basic authentication:
#!/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 pyOpenSSL 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)
# alternative credential storage implementations: https://twistedmatrix.com/documents/current/api/twisted.cred.checkers.ICredentialsChecker.html
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)
Here is an example of a twisted server running on SSL with basic authentication:
#!/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 pyOpenSSL 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)
# alternative credential storage implementations: https://twistedmatrix.com/documents/current/api/twisted.cred.checkers.ICredentialsChecker.html
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)
edited Aug 17 at 10:06
answered Jul 14 '16 at 19:41
ccpizza
12.1k48182
12.1k48182
add a comment |
add a comment |
up vote
2
down vote
HTTPS is HTTP over SSL. HTTPS = HTTP + SSL/TLS. See document http://twistedmatrix.com/documents/13.0.0/core/howto/ssl.html#auto1 and http://twistedmatrix.com/documents/13.0.0/web/howto/using-twistedweb.html#auto2
This is the simple example:
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()
add a comment |
up vote
2
down vote
HTTPS is HTTP over SSL. HTTPS = HTTP + SSL/TLS. See document http://twistedmatrix.com/documents/13.0.0/core/howto/ssl.html#auto1 and http://twistedmatrix.com/documents/13.0.0/web/howto/using-twistedweb.html#auto2
This is the simple example:
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()
add a comment |
up vote
2
down vote
up vote
2
down vote
HTTPS is HTTP over SSL. HTTPS = HTTP + SSL/TLS. See document http://twistedmatrix.com/documents/13.0.0/core/howto/ssl.html#auto1 and http://twistedmatrix.com/documents/13.0.0/web/howto/using-twistedweb.html#auto2
This is the simple example:
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 is HTTP over SSL. HTTPS = HTTP + SSL/TLS. See document http://twistedmatrix.com/documents/13.0.0/core/howto/ssl.html#auto1 and http://twistedmatrix.com/documents/13.0.0/web/howto/using-twistedweb.html#auto2
This is the simple example:
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()
edited Nov 20 at 16:04
answered Mar 1 '17 at 6:39
xxg
850510
850510
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f5210527%2ftwisted-over-https%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown