SO_KEEPALIVE
if tcp_keepalive: # 启用KeepAlive选项 sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) # TCP_KEEPCNT:判定断开前的KeepAlive探测次数 if tcp_keepcnt: sock.setsockopt(socket.SOL_TCP, socket.TCP_KEEPCNT, tcp_keepcnt) # TCP_KEEPIDLE: 开始首次KeepAlive探测前的TCP空闲时间 if tcp_keepidle: sock.setsockopt(socket.SOL_TCP, socket.TCP_KEEPIDLE, tcp_keepidle) # TCP_KEEPINTVL:两次KeepAlive探测间的时间间隔 if tcp_keepintvl: sock.setsockopt(socket.SOL_TCP, socket.TCP_KEEPINTVL, tcp_keepintvl)