| 1 |
#!/usr/bin/env python |
| 2 |
# Copyright (c) Twisted Matrix Laboratories. |
| 3 |
# See LICENSE for details. |
| 4 |
|
| 5 |
import echoclient |
| 6 |
|
| 7 |
from twisted.internet import defer, endpoints, protocol, ssl, task |
| 8 |
from twisted.python.modules import getModule |
| 9 |
|
| 10 |
|
| 11 |
async def main(reactor): |
| 12 |
factory = protocol.Factory.forProtocol(echoclient.EchoClient) |
| 13 |
certData = getModule(__name__).filePath.sibling("public.pem").getContent() |
| 14 |
authority = ssl.Certificate.loadPEM(certData) |
| 15 |
options = ssl.optionsForClientTLS("example.com", trustRoot=authority) |
| 16 |
tcpEndpoint = endpoints.HostnameEndpoint(reactor, "localhost", 8000) |
| 17 |
endpoint = endpoints.wrapClientTLS(options, tcpEndpoint, reactor) |
| 18 |
done = defer.Deferred() |
| 19 |
echoClient = await endpoint.connect(factory) |
| 20 |
echoClient.dataReceived = lambda data: done.callback(data) |
| 21 |
print(await done) |
| 22 |
|
| 23 |
|
| 24 |
if __name__ == "__main__": |
| 25 |
import echoclient_ssl |
| 26 |
|
| 27 |
task.react(echoclient_ssl.main) |