def _connect_socket(self, host, port):
sock = None
error = None
for res in socket.getaddrinfo(host, port, socket.AF_UNSPEC, socket.SOCK_STREAM, socket.IPPROTO_TCP):
af, socktype, proto, canonname, sa = res
try:
sock = socket.socket(af, socktype, proto)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
sock.connect(sa)
return sock
except Exception as e:
error = e
if sock is not None:
sock.close()
break
if error is not None:
raise ConnectionException("connection failed: {}".format(error))
else:
raise ConnectionException("no suitable socket")
评论列表
文章目录