Skip to content
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

Fix for #226. #227

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
54 changes: 34 additions & 20 deletions ws4py/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,11 @@ def __init__(self, url, protocols=None, extensions=None,
# Let's handle IPv4 and IPv6 addresses
# Simplified from CherryPy's code
try:
family, socktype, proto, canonname, sa = socket.getaddrinfo(self.host, self.port,
socket.AF_UNSPEC,
socket.SOCK_STREAM,
0, socket.AI_PASSIVE)[0]
addrinfo = socket.getaddrinfo(self.host, self.port,
socket.AF_UNSPEC,
socket.SOCK_STREAM,
0, socket.AI_PASSIVE)

except socket.gaierror:
family = socket.AF_INET
if self.host.startswith('::'):
Expand All @@ -107,16 +108,35 @@ def __init__(self, url, protocols=None, extensions=None,
proto = 0
canonname = ""
sa = (self.host, self.port, 0, 0)

sock = socket.socket(family, socktype, proto)
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
if hasattr(socket, 'AF_INET6') and family == socket.AF_INET6 and \
self.host.startswith('::'):

addrinfo = [(family, socktype, proto, canonname, sa)]

for family, socktype, proto, canonname, sa in addrinfo:

sock = socket.socket(family, socktype, proto)
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
if hasattr(socket, 'AF_INET6') and family == socket.AF_INET6 and \
self.host.startswith('::'):
try:
sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0)
except (AttributeError, socket.error):
pass

if self.scheme == "wss":
# default port is now 443; upgrade self.sender to send ssl
sock = ssl.wrap_socket(self.sock, **self.ssl_options)
self._is_secure = True

try:
sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0)
except (AttributeError, socket.error):
pass
sock.connect(self.bind_addr)
break
except socket.error as err:
sock = None
continue

if sock is None:
raise err

WebSocket.__init__(self, sock, protocols=protocols,
extensions=extensions,
Expand Down Expand Up @@ -206,15 +226,9 @@ def close(self, code=1000, reason=''):

def connect(self):
"""
Connects this websocket and starts the upgrade handshake
Starts the upgrade handshake
with the remote endpoint.
"""
if self.scheme == "wss":
# default port is now 443; upgrade self.sender to send ssl
self.sock = ssl.wrap_socket(self.sock, **self.ssl_options)
self._is_secure = True

self.sock.connect(self.bind_addr)

self._write(self.handshake_request)

Expand Down