toolbelt
toolbelt copied to clipboard
TCPKeepAliveAdapter doesn't work on OSX
I found that the TCPKeepAliveAdapter wasn't working on OSX (Python 3.4) due to missing values socket.TCP_KEEPINTVL and socket.TCP_KEEPCNT. Also, it doesn't set TCP_KEEPALIVE. See the answer on https://stackoverflow.com/questions/12248132/how-to-change-tcp-keepalive-timer-using-python-script
The following works for me:
class TCPKeepAliveAdapter(SocketOptionsAdapter):
def __init__(self, **kwargs):
socket_options = kwargs.pop('socket_options',
socket_options_adapters.SocketOptionsAdapter.default_options)
idle = kwargs.pop('idle', 60)
interval = kwargs.pop('interval', 20)
count = kwargs.pop('count', 5)
socket_options = socket_options + [
(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
]
# NOTE(Ian): Apparently OSX does not have this constant defined, so we
# set it conditionally.
if getattr(socket, 'TCP_KEEPIDLE', None) is not None:
socket_options += [(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, idle)]
if getattr(socket, 'TCP_KEEPINTVL', None) is not None:
socket_options += [(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, interval)]
else:
# For OSX:
# scraped from /usr/include, not exported by python's socket module
# TCP_KEEPALIVE = 0x10
socket_options += [(socket.IPPROTO_TCP, 0x10, interval)]
if getattr(socket, 'TCP_KEEPCNT', None) is not None:
socket_options += [(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, count)]
super(TCPKeepAliveAdapter, self).__init__(
socket_options=socket_options, **kwargs
)
Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.
Related to #98. I'd be very happy to merge a pull request that fixes this
I added a PR https://github.com/sigmavirus24/requests-toolbelt/pull/188
I'm working on some code that relies on this change to the requests_toolbelt package. I can use the latest version from the repo, but is there an idea for when the package in PyPI will be updated?
I'm having the same issue here. Do you have any estimated date for the next release with this fix?