8000 Add limited TLS capability to source/sink examples. · Python-Repository-Hub/asyncio@a4f2204 · GitHub
[go: up one dir, main page]

Skip to content

Commit a4f2204

Browse files
committed
Add limited TLS capability to source/sink examples.
1 parent 53e4794 commit a4f2204

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

examples/sink.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
"""Test service that accepts connections and reads all data off them."""
22

33
import argparse
4+
import os
45
import sys
56

67
from asyncio import *
78

89
ARGS = argparse.ArgumentParser(description="TCP data sink example.")
10+
ARGS.add_argument(
11+
'--tls', action='store_true', dest='tls',
12+
default=False, help='Use TLS with a self-signed cert')
913
ARGS.add_argument(
1014
'--iocp', action='store_true', dest='iocp',
1115
default=False, help='Use IOCP event loop (Windows only)')
@@ -54,8 +58,20 @@ def connection_lost(self, how):
5458
@coroutine
5559
def start(loop, host, port):
5660
global server
57-
server = yield from loop.create_server(Service, host, port)
58-
dprint('serving', [s.getsockname() for s in server.sockets])
61+
sslctx = None
62+
if args.tls:
63+
import ssl
64+
# TODO: take cert/key from args as well.
65+
here = os.path.join(os.path.dirname(__file__), '..', 'tests')
66+
sslctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
67+
sslctx.options |= ssl.OP_NO_SSLv2
68+
sslctx.load_cert_chain(
69+
certfile=os.path.join(here, 'sample.crt'),
70+
keyfile=os.path.join(here, 'sample.key'))
71+
72+
server = yield from loop.create_server(Service, host, port, ssl=sslctx)
73+
dprint('serving TLS' if sslctx else 'serving',
74+
[s.getsockname() for s in server.sockets])
5975
yield from server.wait_closed()
6076

6177

examples/source.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44
import sys
55

66
from asyncio import *
7+
from asyncio import test_utils
78

89

910
ARGS = argparse.ArgumentParser(description="TCP data sink example.")
11+
ARGS.add_argument(
12+
'--tls', action='store_true', dest='tls',
13+
default=False, help='Use TLS')
1014
ARGS.add_argument(
1115
'--iocp', action='store_true', dest='iocp',
1216
default=False, help='Use IOCP event loop (Windows only)')
@@ -67,7 +71,11 @@ def connection_lost(self, exc):
6771

6872
@coroutine
6973
def start(loop, host, port):
70-
tr, pr = yield from loop.create_connection(Client, host, port)
74+
sslctx = None
75+
if args.tls:
76+
sslctx = test_utils.dummy_ssl_context()
77+
tr, pr = yield from loop.create_connection(Client, host, port,
78+
ssl=sslctx)
7179
dprint('tr =', tr)
7280
dprint('pr =', pr)
7381
yield from pr.waiter

examples/source1.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33
import argparse
44
import sys
55

6-
from tulip import *
6+
from asyncio import *
7+
from asyncio import test_utils
78

89
ARGS = argparse.ArgumentParser(description="TCP data sink example.")
10+
ARGS.add_argument(
11+
'--tls', action='store_true', dest='tls',
12+
default=False, help='Use TLS')
913
ARGS.add_argument(
1014
'--iocp', action='store_true', dest='iocp',
1115
default=False, help='Use IOCP event loop (Windows only)')
@@ -49,7 +53,11 @@ def oprint(self, *args):
4953
def start(loop, args):
5054
d = Debug()
5155
total = 0
52-
r, w = yield from open_connection(args.host, args.port)
56+
sslctx = None
57+
if args.tls:
58+
d.print('using dummy SSLContext')
59+
sslctx = test_utils.dummy_ssl_context()
60+
r, w = yield from open_connection(args.host, args.port, ssl=sslctx)
5361
d.print('r =', r)
5462
d.print('w =', w)
5563
if args.stop:
@@ -75,7 +83,7 @@ def main():
7583
global args
7684
args = ARGS.parse_args()
7785
if args.iocp:
78-
from tulip.windows_events import ProactorEventLoop
86+
from asyncio.windows_events import ProactorEventLoop
7987
loop = ProactorEventLoop()
8088
set_event_loop(loop)
8189
else:

0 commit comments

Comments
 (0)
0