<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><br><div><div>On Oct 7, 2014, at 3:21 PM, Carl D'Halluin <<a href="mailto:carl@amplidata.com">carl@amplidata.com</a>> wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><br><blockquote type="cite">Another problem is that Twisted doesn't yet support SSL on adopted sockets.<br></blockquote><br>I did this by hand:<br><br># Suppose your create/bind/list your listen_socket, and<br># its file descriptor is listen_socket_fd<br><br><br>site = server.Site(MyHttpsSite())<br><br>cert = '/path/to/my/cert'<br>key = '/path/to/my/key'<br><br>ctx = DefaultOpenSSLContextFactory(key, cert)<br>tlsFactory = tls.TLSMemoryBIOFactory(ctx, False, site)<br>p = tcp.Port._fromListeningDescriptor(reactor,<br>                                             listen_socket_fd,<br>                                             socket.AF_INET,<br>                                             tlsFactory)<br>p._type = 'TLS'<br>p.startListening()<br><br>os.close(listen_socket_fd)<br>reactor.run()</blockquote><br></div><div>There is work underway for addressing this particular use-case (endpoint composition) via string endpoints:</div><div><br></div><div><<a href="https://twistedmatrix.com/trac/ticket/5642">https://twistedmatrix.com/trac/ticket/5642</a>></div><div><br></div><div>But even today you don't have to touch unsupported private APIs to do this.</div><div><br></div><div>As per <<a href="https://twistedmatrix.com/trac/wiki/CompatibilityPolicy">https://twistedmatrix.com/trac/wiki/CompatibilityPolicy</a>> we really like to discourage people from touching private (i.e. underscore-prefixed or imported-from-another-module) API, because it may well break in the next release and you'll have no recourse.</div><div><br></div><div>(Plus, you should really be using CertificateOptions, not DefaultOpenSSLContextFactory, either via PrivateCertificate(...).options() or directly constructed.)</div><div><br></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"><div>from twisted.python.filepath import FilePath</div><div>site = server.Site(MyHttpsSite())</div><div><br></div><div>cert = FilePath('/path/to/my/cert').getContent()</div><div>key = FilePath('/path/to/my/key').getContent()</div><div><br></div><div>from twisted.internet.ssl import PrivateCertificate</div><div>certificateWithKey = PrivateCertificate.loadPEM(b"\n".join([cert, key]))</div><div><br></div><div><div>tlsFactory = tls.TLSMemoryBIOFactory(certificateWithKey.options(), False, site)</div></div><div><br></div><div>import socket</div><div>from twisted.internet import reactor</div><div>reactor.adoptStreamPort(listen_socket_fd, socket.AF_INET, tlsFactory)</div><div>import os</div><div>os.close(listen_socket_fd)</div><div>reactor.run()</div></blockquote><span></span><div><br></div><div>This code hasn't been tested, but no underscores should be necessary!</div><div><br></div><div>-glyph</div><div><br></div></body></html>