Skip to content

Commit a7505cf

Browse files
Allow self signed cert (#83)
added a parameter S2Connection to allow self signed certificates
1 parent eb547cb commit a7505cf

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/s2python/s2_connection.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import time
55
import threading
66
import uuid
7+
import ssl
78
from dataclasses import dataclass
89
from 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

Comments
 (0)