sockets: How to know when your data is sent

Ron Samuel Klatchko moocat94122 at
Thu Nov 11 01:55:42 CET 2004

mederis at (Marc Ederis) wrote in message news:<f00811ba.0411090653.5f6d1d29 at>...
> I'm having a problem with sending data with sockets over a dial-up
> connection. When I use the send function, it will happily send a
> buffer of a megabyte and more in one shot. But of course, the data is
> still in the network buffer... Meaning you can't disconnect for awhile
> (but for how long...). The problem is, how can I know when it's done?
> Is there a way to be notified when the data has truly been sent?

You can get pretty close if you are willing to close the socket (but 
since you want to disconnect, that should be fine).

What you need to do is turn on linger mode with a very large timeout (large
enough so that your slow connection has enough time to send the data).
Once linger is turned on, close() will block until all the data has
been sent.

The one issue that you can run into is if the linger time runs out before
all the data is sent then the rest of the data is discarded.  But as long
as you set linger long enough (how about 10 years), you will run out of
patience long before the data is discarded.


More information about the Python-list mailing list