diff options
| author | Guido van Rossum <guido@python.org> | 2013-11-01 15:51:10 -0700 |
|---|---|---|
| committer | Guido van Rossum <guido@python.org> | 2013-11-01 15:51:10 -0700 |
| commit | 04911d977abf5ac3fd9d725a674b67e18fb23acb (patch) | |
| tree | 062e1ecfecaac30e341201b15ed4fc494244f001 /examples | |
| parent | 2205d8d2c68136450f64bd6af95536c89497e067 (diff) | |
| download | trollius-04911d977abf5ac3fd9d725a674b67e18fb23acb.tar.gz | |
Add limited TLS capability to source/sink examples.
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/sink.py | 20 | ||||
| -rw-r--r-- | examples/source.py | 10 | ||||
| -rw-r--r-- | examples/source1.py | 14 |
3 files changed, 38 insertions, 6 deletions
diff --git a/examples/sink.py b/examples/sink.py index b5edc3a..4b223fd 100644 --- a/examples/sink.py +++ b/examples/sink.py @@ -1,12 +1,16 @@ """Test service that accepts connections and reads all data off them.""" import argparse +import os import sys from asyncio import * ARGS = argparse.ArgumentParser(description="TCP data sink example.") ARGS.add_argument( + '--tls', action='store_true', dest='tls', + default=False, help='Use TLS with a self-signed cert') +ARGS.add_argument( '--iocp', action='store_true', dest='iocp', default=False, help='Use IOCP event loop (Windows only)') ARGS.add_argument( @@ -54,8 +58,20 @@ class Service(Protocol): @coroutine def start(loop, host, port): global server - server = yield from loop.create_server(Service, host, port) - dprint('serving', [s.getsockname() for s in server.sockets]) + sslctx = None + if args.tls: + import ssl + # TODO: take cert/key from args as well. + here = os.path.join(os.path.dirname(__file__), '..', 'tests') + sslctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23) + sslctx.options |= ssl.OP_NO_SSLv2 + sslctx.load_cert_chain( + certfile=os.path.join(here, 'sample.crt'), + keyfile=os.path.join(here, 'sample.key')) + + server = yield from loop.create_server(Service, host, port, ssl=sslctx) + dprint('serving TLS' if sslctx else 'serving', + [s.getsockname() for s in server.sockets]) yield from server.wait_closed() diff --git a/examples/source.py b/examples/source.py index c36f147..9aff8c1 100644 --- a/examples/source.py +++ b/examples/source.py @@ -4,10 +4,14 @@ import argparse import sys from asyncio import * +from asyncio import test_utils ARGS = argparse.ArgumentParser(description="TCP data sink example.") ARGS.add_argument( + '--tls', action='store_true', dest='tls', + default=False, help='Use TLS') +ARGS.add_argument( '--iocp', action='store_true', dest='iocp', default=False, help='Use IOCP event loop (Windows only)') ARGS.add_argument( @@ -67,7 +71,11 @@ class Client(Protocol): @coroutine def start(loop, host, port): - tr, pr = yield from loop.create_connection(Client, host, port) + sslctx = None + if args.tls: + sslctx = test_utils.dummy_ssl_context() + tr, pr = yield from loop.create_connection(Client, host, port, + ssl=sslctx) dprint('tr =', tr) dprint('pr =', pr) yield from pr.waiter diff --git a/examples/source1.py b/examples/source1.py index 6471d81..b8f8979 100644 --- a/examples/source1.py +++ b/examples/source1.py @@ -3,10 +3,14 @@ import argparse import sys -from tulip import * +from asyncio import * +from asyncio import test_utils ARGS = argparse.ArgumentParser(description="TCP data sink example.") ARGS.add_argument( + '--tls', action='store_true', dest='tls', + default=False, help='Use TLS') +ARGS.add_argument( '--iocp', action='store_true', dest='iocp', default=False, help='Use IOCP event loop (Windows only)') ARGS.add_argument( @@ -49,7 +53,11 @@ class Debug: def start(loop, args): d = Debug() total = 0 - r, w = yield from open_connection(args.host, args.port) + sslctx = None + if args.tls: + d.print('using dummy SSLContext') + sslctx = test_utils.dummy_ssl_context() + r, w = yield from open_connection(args.host, args.port, ssl=sslctx) d.print('r =', r) d.print('w =', w) if args.stop: @@ -75,7 +83,7 @@ def main(): global args args = ARGS.parse_args() if args.iocp: - from tulip.windows_events import ProactorEventLoop + from asyncio.windows_events import ProactorEventLoop loop = ProactorEventLoop() set_event_loop(loop) else: |
