'Depending on the combination of python version and whether we\'re
connecting over http or https, we might need to access _sock, which
may or may not exist; or we may need to just settimeout on socket
itself, which also may or may not have settimeout on it. To avoid
missing the correct one, we try both.
We also do not want to set the timeout if it is already disabled, as
you run the risk of changing a socket that was non-blocking to
blocking, for example when using gevent.'

def _disable_socket_timeout(self, socket):

    sockets = [socket, getattr(socket, '_sock', None)]
    for s in sockets:
        if (not hasattr(s, 'settimeout')):
            continue
        timeout = (-1)
        if hasattr(s, 'gettimeout'):
            timeout = s.gettimeout()
        if ((timeout is None) or (timeout == 0.0)):
            continue
        s.settimeout(None)
