Reviewers: ,
Please review this at http://codereview.tryton.org/57002/
Affected files:
M tryton/pysocket.py
Index: tryton/pysocket.py
===================================================================
--- a/tryton/pysocket.py
+++ b/tryton/pysocket.py
@@ -55,6 +55,26 @@
else:
self.ca_certs = None
+ def _get_socket(self, host, port):
+ sock = None
+ for res in socket.getaddrinfo(host, int(port), socket.AF_UNSPEC,
+ socket.SOCK_STREAM):
+ af, socktype, proto, canonname, sa = res
+ try:
+ sock = socket.socket(af, socktype, proto)
+ except socket.error:
+ sock = None
+ continue
+ sock.settimeout(CONNECT_TIMEOUT)
+ try:
+ sock.connect(sa)
+ except socket.error:
+ sock.close()
+ sock = None
+ continue
+ break
+ return sock
+
def connect(self, host, port=False):
if not port:
buf = host.split('//')[1]
@@ -62,43 +82,21 @@
hostname = host
if host in DNS_CACHE:
host = DNS_CACHE[host]
- self.sock = None
- if socket.has_ipv6:
+ self.sock = self._get_socket(host, int(port))
+ DNS_CACHE[hostname], port = self.sock.getpeername()[:2]
+ sock = self._get_socket(host, int(port))
+ if ssl:
try:
- socket.getaddrinfo(host, int(port), socket.AF_INET6)
- self.sock = socket.socket(socket.AF_INET6,
- socket.SOCK_STREAM)
- self.sock.settimeout(CONNECT_TIMEOUT)
- self.sock.connect((host, int(port)))
- except socket.error:
- self.sock = None
- if self.sock is None:
- self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- self.sock.settimeout(CONNECT_TIMEOUT)
- self.sock.connect((host, int(port)))
- DNS_CACHE[hostname], port = self.sock.getpeername()[:2]
- try:
- sock = None
- if socket.has_ipv6:
- try:
- socket.getaddrinfo(host, int(port), socket.AF_INET6)
- sock = socket.socket(socket.AF_INET6,
socket.SOCK_STREAM)
- sock.settimeout(CONNECT_TIMEOUT)
- sock.connect((host, int(port)))
- except socket.error:
- sock = None
- if sock is None:
- sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- sock.settimeout(CONNECT_TIMEOUT)
- sock.connect((host, int(port)))
- if ssl:
ssl_sock = ssl.wrap_socket(sock)
self.ssl = True
- elif hasattr(socket, 'ssl'):
+ except ssl.SSLError:
+ pass
+ elif hasattr(socket, 'ssl'):
+ try:
ssl_sock = socket.ssl(sock)
self.ssl = True
- except socket.error:
- pass
+ except socket.error:
+ pass
self.sock.settimeout(TIMEOUT)
fingerprint = None
if self.ssl:
--
[email protected] mailing list