44import time
55import threading
66import uuid
7+ import ssl
78from dataclasses import dataclass
89from typing import Optional , List , Type , Dict , Callable , Awaitable , Union
910
@@ -198,6 +199,7 @@ class S2Connection: # pylint: disable=too-many-instance-attributes
198199 _eventloop : asyncio .AbstractEventLoop
199200 _stop_event : asyncio .Event
200201 _restart_connection_event : asyncio .Event
202+ _verify_certificate : bool
201203
202204 def __init__ ( # pylint: disable=too-many-arguments
203205 self ,
@@ -206,6 +208,7 @@ def __init__( # pylint: disable=too-many-arguments
206208 control_types : List [S2ControlType ],
207209 asset_details : AssetDetails ,
208210 reconnect : bool = False ,
211+ verify_certificate : bool = True ,
209212 ) -> None :
210213 self .url = url
211214 self .reconnect = reconnect
@@ -221,6 +224,7 @@ def __init__( # pylint: disable=too-many-arguments
221224 self .control_types = control_types
222225 self .role = role
223226 self .asset_details = asset_details
227+ self ._verify_certificate = verify_certificate
224228
225229 self ._handlers .register_handler (SelectControlType , self .handle_select_control_type_as_rm )
226230 self ._handlers .register_handler (Handshake , self .handle_handshake )
@@ -318,8 +322,13 @@ async def wait_till_connection_restart() -> None:
318322 await self .ws .wait_closed ()
319323
320324 async def _connect_ws (self ) -> None :
325+ ssl_context = ssl .SSLContext (ssl .PROTOCOL_TLS_CLIENT )
326+ if not self ._verify_certificate :
327+ ssl_context .check_hostname = False
328+ ssl_context .verify_mode = ssl .CERT_NONE
329+
321330 try :
322- self .ws = await ws_connect (uri = self .url )
331+ self .ws = await ws_connect (uri = self .url , ssl = ssl_context )
323332 except (EOFError , OSError ) as e :
324333 logger .info ("Could not connect due to: %s" , str (e ))
325334
0 commit comments