@@ -169,14 +169,21 @@ class TcpClient(TcpServerClientBase):
169
169
TCP client base.
170
170
"""
171
171
172
- def __init__ (self , ctx , endpoint , api = None , connectionClass = TcpConnection ):
172
+ def __init__ (self , ctx , endpoint , api = None , connectionClass = TcpConnection , useSsl = False , sslKeyCert = None ):
173
173
"""Create a TCP client.
174
174
175
175
:param endpoint: a tuple of (hostname, port) to connect to
176
176
:param api: an api object to receive callback on
177
177
:param connectionClass: the class to create for each TCP connection
178
178
"""
179
- super (TcpClient , self ).__init__ (ctx , endpoint = endpoint , api = api , connectionClass = connectionClass )
179
+ sslContext = None
180
+ if useSsl or sslKeyCert is not None :
181
+ sslContext = ssl .create_default_context (ssl .Purpose .SERVER_AUTH )
182
+ sslContext .check_hostname = False
183
+ sslContext .verify_mode = ssl .CERT_NONE
184
+ if sslKeyCert is not None :
185
+ sslContext .load_cert_chain (sslKeyCert )
186
+ super (TcpClient , self ).__init__ (ctx , endpoint = endpoint , api = api , connectionClass = connectionClass , sslContext = sslContext )
180
187
self ._ctx .RegisterClient (self )
181
188
182
189
def Destroy (self ):
@@ -309,10 +316,10 @@ def SpinOnce(self, timeout=0):
309
316
clientSocket = None
310
317
try :
311
318
clientSocket = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
312
- clientSocket .connect (client ._endpoint )
313
- log .debug ('new connection to %s' , client ._endpoint )
314
319
if client ._sslContext is not None :
315
320
clientSocket = client ._sslContext .wrap_socket (clientSocket , server_side = False )
321
+ clientSocket .connect (client ._endpoint )
322
+ log .debug ('new connection to %s' , client ._endpoint )
316
323
clientSocket .setblocking (0 ) # TODO: deferred non-blocking after connect finishes, not ideal
317
324
except Exception as e :
318
325
if clientSocket :
0 commit comments