From: J. Alexander Treuman Date: Sun, 23 Mar 2008 20:47:06 +0000 (-0400) Subject: mpd.py: loop over addresses returned by getaddrinfo to connect to X-Git-Tag: v0.2.0~8 X-Git-Url: https://git.kaliko.me/?a=commitdiff_plain;h=3bbc456dc51c95e8ad5bb4e270d781ed370bfe27;p=python-musicpd.git mpd.py: loop over addresses returned by getaddrinfo to connect to This allows us to support IPv6 and multi-homed hostnames. getaddrinfo is called with the same flags as libmpdclient uses, making address resolution consistent between the two. --- diff --git a/mpd.py b/mpd.py index ce1784b..ae1f2b3 100644 --- a/mpd.py +++ b/mpd.py @@ -289,8 +289,22 @@ class MPDClient(object): def connect(self, host, port): if self._sock: raise ConnectionError, "Already connected" - self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - self._sock.connect((host, port)) + msg = "getaddrinfo returns an empty list" + for res in socket.getaddrinfo(host, port, socket.AF_UNSPEC, + socket.SOCK_STREAM, socket.IPPROTO_TCP, + socket.AI_ADDRCONFIG): + af, socktype, proto, canonname, sa = res + try: + self._sock = socket.socket(af, socktype, proto) + self._sock.connect(sa) + except socket.error, msg: + if self._sock: + self._sock.close() + self._sock = None + continue + break + if not self._sock: + raise socket.error, msg self._rfile = self._sock.makefile("rb") self._wfile = self._sock.makefile("wb") self._hello()