2
2
import asyncio
3
3
import gc
4
4
import os .path
5
+ import pathlib
5
6
import socket
7
+ import ssl
6
8
7
9
8
10
PRINT = 0
@@ -86,6 +88,7 @@ async def print_debug(loop):
86
88
parser .add_argument ('--proto' , default = False , action = 'store_true' )
87
89
parser .add_argument ('--addr' , default = '127.0.0.1:25000' , type = str )
88
90
parser .add_argument ('--print' , default = False , action = 'store_true' )
91
+ parser .add_argument ('--ssl' , default = False , action = 'store_true' )
89
92
args = parser .parse_args ()
90
93
91
94
if args .uvloop :
@@ -119,6 +122,19 @@ async def print_debug(loop):
119
122
120
123
print ('serving on: {}' .format (addr ))
121
124
125
+ server_context = None
126
+ if args .ssl :
127
+ print ('with SSL' )
128
+ server_context = ssl .SSLContext (ssl .PROTOCOL_SSLv23 )
129
+ server_context .load_cert_chain (
130
+ (pathlib .Path (__file__ ).parent .parent .parent /
131
+ 'tests' / 'certs' / 'ssl_cert.pem' ),
132
+ (pathlib .Path (__file__ ).parent .parent .parent /
133
+ 'tests' / 'certs' / 'ssl_key.pem' ))
134
+ if hasattr (server_context , 'check_hostname' ):
135
+ server_context .check_hostname = False
136
+ server_context .verify_mode = ssl .CERT_NONE
137
+
122
138
if args .streams :
123
139
if args .proto :
124
140
print ('cannot use --stream and --proto simultaneously' )
@@ -127,10 +143,12 @@ async def print_debug(loop):
127
143
print ('using asyncio/streams' )
128
144
if unix :
129
145
coro = asyncio .start_unix_server (echo_client_streams ,
130
- addr , loop = loop )
146
+ addr , loop = loop ,
147
+ ssl = server_context )
131
148
else :
132
149
coro = asyncio .start_server (echo_client_streams ,
133
- * addr , loop = loop )
150
+ * addr , loop = loop ,
151
+ ssl = server_context )
134
152
srv = loop .run_until_complete (coro )
135
153
elif args .proto :
136
154
if args .streams :
@@ -139,11 +157,17 @@ async def print_debug(loop):
139
157
140
158
print ('using simple protocol' )
141
159
if unix :
142
- coro = loop .create_unix_server (EchoProtocol , addr )
160
+ coro = loop .create_unix_server (EchoProtocol , addr ,
161
+ ssl = server_context )
143
162
else :
144
- coro = loop .create_server (EchoProtocol , * addr )
163
+ coro = loop .create_server (EchoProtocol , * addr ,
164
+ ssl = server_context )
145
165
srv = loop .run_until_complete (coro )
146
166
else :
167
+ if args .ssl :
168
+ print ('cannot use SSL for loop.sock_* methods' )
169
+ exit (1 )
170
+
147
171
print ('using sock_recv/sock_sendall' )
148
172
loop .create_task (echo_server (loop , addr , unix ))
149
173
try :
0 commit comments