In my python script, I have activate TCP Keepalive using this command:
x = s.setsockopt( socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
My goal is for socket connection to get closed, if there is no transmission(*) for 5 minutes. I am working on Windows and my python script is only receiving and not transmitting any data to client program.
What I know is, by default, if no transmission will be there for 2 hours, then only I can close the connection using try and except. I know, for windows, I can manually reduce this waiting time by going to registry. But is there is a way by which, I can modify it from my script?
(*) here "no transmission" means "something quietly eats packets on the network" rather than "I'm not trying to send anything."
For windows, in python:
This will enable socket keep alive, with a 10 second keep alive time and a 3 second keep alive interval.
More detailed info here
You can set the TCP keepalive timers on an already-open socket using setsockopt().
For equivalent options on windows refer to msdn. Looking through the Python source, it seems you need to set
SO_KEEPALIVE
withsock.setsockopt
similar to in Unix, and [optionally?] setSIO_KEEPALIVE_VALS
withsock.ioctl
.Keepalive is a TCP protocol trick to detect dead connections, e.g. for a Telnet server. It is off by default. To change keep alive timers use
socket.setsockopt
. See https://docs.python.org/2/library/socket.html