code: def set_keep_alive(self, sock, after_idle_sec=5, interval_sec=60,
                         max_fails=5):
          
  
          # OSX
          if platform.system() == "Darwin":
              # scraped from /usr/include, not exported by python's socket module
              TCP_KEEPALIVE = 0x10
              sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
              sock.setsockopt(socket.IPPROTO_TCP, TCP_KEEPALIVE, interval_sec)
  
          if platform.system() == "Windows":
              sock.ioctl(socket.SIO_KEEPALIVE_VALS, (1, 10000, 3000))
  
          if platform.system() == "Linux":
              sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
              sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE,
                              after_idle_sec)
              sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL,
                              interval_sec)
              sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, max_fails)
docs: This function instructs the TCP socket to send a heart beat every n
          seconds to detect dead connections. It's the TCP equivalent of the
          IRC ping-pong protocol and allows for better cleanup / detection
          of dead TCP connections.
  
          It activates after 1 second (after_idle_sec) of idleness, then sends
          a keepalive ping once every 3 seconds(interval_sec), and closes the
          connection after 5 failed ping (max_fails), or 15 seconds