[Pkg-mpd-commits] [python-mpd] 130/262: Add optional connection timeout parameter

Simon McVittie smcv at debian.org
Sun May 22 18:16:36 UTC 2016


This is an automated email from the git hooks/post-receive script.

smcv pushed a commit to branch upstream
in repository python-mpd.

commit 39047acf1c9af21cc39566365b4a759faa54c8be
Author: Michael Helmling <helmling at mathematik.uni-kl.de>
Date:   Fri Mar 30 22:46:59 2012 +0200

    Add optional connection timeout parameter
---
 mpd.py  | 12 +++++++-----
 test.py |  5 +++++
 2 files changed, 12 insertions(+), 5 deletions(-)

diff --git a/mpd.py b/mpd.py
index 5bf177e..617a3c5 100644
--- a/mpd.py
+++ b/mpd.py
@@ -378,15 +378,16 @@ class MPDClient():
         self._rfile = _NotConnected()
         self._wfile = _NotConnected()
 
-    def _connect_unix(self, path):
+    def _connect_unix(self, path, timeout):
         if not hasattr(socket, "AF_UNIX"):
             raise ConnectionError("Unix domain sockets not supported "
                                   "on this platform")
         sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
+        sock.settimeout(timeout)
         sock.connect(path)
         return sock
 
-    def _connect_tcp(self, host, port):
+    def _connect_tcp(self, host, port, timeout):
         try:
             flags = socket.AI_ADDRCONFIG
         except AttributeError:
@@ -400,6 +401,7 @@ class MPDClient():
             try:
                 sock = socket.socket(af, socktype, proto)
                 sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
+                sock.settimeout(timeout)
                 sock.connect(sa)
                 return sock
             except socket.error as e:
@@ -411,13 +413,13 @@ class MPDClient():
         else:
             raise ConnectionError("getaddrinfo returns an empty list")
 
-    def connect(self, host, port):
+    def connect(self, host, port, timeout=None):
         if self._sock is not None:
             raise ConnectionError("Already connected")
         if host.startswith("/"):
-            self._sock = self._connect_unix(host)
+            self._sock = self._connect_unix(host, timeout)
         else:
-            self._sock = self._connect_tcp(host, port)
+            self._sock = self._connect_tcp(host, port, timeout)
         self._rfile = self._sock.makefile("r")
         self._wfile = self._sock.makefile("w")
         try:
diff --git a/test.py b/test.py
index 5c93837..ad3bc3c 100755
--- a/test.py
+++ b/test.py
@@ -197,5 +197,10 @@ class TestMPDClient(unittest.TestCase):
     def test_numbers_as_command_args(self):
         res = self.client.find("file", 1)
 
+    def test_timeout(self):
+        self.client.disconnect()
+        self.client.connect(TEST_MPD_HOST, TEST_MPD_PORT, timeout=5)
+        self.assertEqual(self.client._sock.gettimeout(), 5)
+
 if __name__ == '__main__':
     unittest.main()

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-mpd/python-mpd.git



More information about the Pkg-mpd-commits mailing list