| 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 |
authData = getModule(__name__).filePath.sibling("server.pem").getContent() |
| 15 |
clientCertificate = ssl.PrivateCertificate.loadPEM(authData) |
| 16 |
authority = ssl.Certificate.loadPEM(certData) |
| 17 |
options = ssl.optionsForClientTLS( |
| 18 |
"example.com", |
| 19 |
trustRoot=authority, |
| 20 |
clientCertificate=clientCertificate, |
| 21 |
) |
| 22 |
tcpEndpoint = endpoints.HostnameEndpoint(reactor, "localhost", 8000) |
| 23 |
endpoint = endpoints.wrapClientTLS(options, tcpEndpoint, reactor) |
| 24 |
done = defer.Deferred() |
| 25 |
echoClient = await endpoint.connect(factory) |
| 26 |
echoClient.dataReceived = lambda data: done.callback(data) |
| 27 |
print(await done) |
| 28 |
|
| 29 |
|
| 30 |
if __name__ == "__main__": |
| 31 |
import ssl_clientauth_client |
| 32 |
|
| 33 |
task.react(ssl_clientauth_client.main) |