I'm trying to determine if a server socket was shut down gracefully or not in python.
The program received files and if some error occurs during the send the file should be removed.
I know your first suggestion will be to let the client send either the length of the file or some terminating data to let the server know that the file transfer is complete.
Sadly, I cannot change the client, it just dumps the file over raw tcp and there is nothing I can do about it. So please, no "but if you could change it" or "your protocol is flawed", I did not write the protocol and I will have to deal with it.
The script should work on both OSX and linux so epoll is out.
Any suggestions?
I can add that I don't care what error occurs, just that any error did.
If the other end has finished sending data, they should shutdown (2) the socket for writing. Then in your multiplexing loop you would be able to check if the socket has been shutdown for writing or has just be closed (writing & reading) by the remote end. This is a graceful shutdown and you can use select/poll/kqueue or whatever.
If the client doesn't do that, doesn't send terminating character and doesn't send the lenght or a checksum it looks hard.
You should drop more detail about the protocol, does the client opens a TCP socket for the sole purpose of sending a file ? Does it close the socket if the transfer is complete ?
In the worst case scenario you would have to check the integrity of the file, if you know its format you can try to check it during run time or have a cron tab do the cleaning. If you cannot, well it will be hard to find a solution. Good luck...
Detecting graceful shut downs of sockets in python