Skip to content

Added tls support for validating certificates #285

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions ws4py/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,15 +206,20 @@ def connect(self):
"""
if self.scheme == "wss":
# default port is now 443; upgrade self.sender to send ssl
context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
protocol = getattr(ssl, 'PROTOCOL_TLS_CLIENT', ssl.PROTOCOL_TLSv1_2) # PROTOCOL_TLS_CLIENT is correct for newer Python but doesn't exist on older Pythons
context = ssl.SSLContext(protocol)
if self.ssl_options.get('certfile', None):
context.load_cert_chain(self.ssl_options.get('certfile'), self.ssl_options.get('keyfile'))

if self.ssl_options.get('ca_certs'):
context.load_verify_locations(self.ssl_options['ca_certs'])

# Prevent check_hostname requires server_hostname (ref #187)
if "cert_reqs" not in self.ssl_options:
context.check_hostname = False
context.verify_mode = ssl.CERT_NONE

self.sock = context.wrap_socket(self.sock)
self.sock = context.wrap_socket(self.sock, server_hostname=self.host)
self._is_secure = True

self.sock.connect(self.bind_addr)
Expand Down
Loading