diff options
author | Marko Kreen | 2013-06-21 09:24:46 +0000 |
---|---|---|
committer | Marko Kreen | 2013-06-21 09:24:46 +0000 |
commit | e9e80bece71ccf5e2a18f2220b9457471db7689f (patch) | |
tree | fd195c4eea3fee008912ac9ed2e5896822f574ae | |
parent | a1242c473d04ed2dc69d82fb371f64808dfced79 (diff) |
set_tcp_keepalive: accept socket object directly
-rw-r--r-- | python/skytools/sockutil.py | 11 |
1 files changed, 7 insertions, 4 deletions
diff --git a/python/skytools/sockutil.py b/python/skytools/sockutil.py index f950f5a0..dbcd021b 100644 --- a/python/skytools/sockutil.py +++ b/python/skytools/sockutil.py @@ -37,10 +37,13 @@ def set_tcp_keepalive(fd, keepalive = True, if not hasattr(socket, 'SO_KEEPALIVE') or not hasattr(socket, 'fromfd'): return - # get numeric fd and cast to socket - if hasattr(fd, 'fileno'): - fd = fd.fileno() - s = socket.fromfd(fd, socket.AF_INET, socket.SOCK_STREAM) + # need socket object + if isinstance(fd, socket.SocketType): + s = fd + else: + if hasattr(fd, 'fileno'): + fd = fd.fileno() + s = socket.fromfd(fd, socket.AF_INET, socket.SOCK_STREAM) # skip if unix socket if type(s.getsockname()) != type(()): |