Best way to "flush" incoming socket data?

Robert Oschler Oschler at earthlink.net
Sun Nov 10 13:29:54 EST 2002


I have a simple Python script that connects to various TCP servers that use
string commands.  Some of them print a banner upon connection some don't.
For the ones that send me a banner string upon connect, what's the best way
to clear out the banner from the receive queue?  Currently I do:

# set timeout value using Timothy O' Malley's timeoutsocket module
#  to something real small.
timeoutsocket.SetDefaultSocketTimeout(3)
dump_banner = sock.recv(1024) # flush banner if any.

# restore default socket timeout to something reasonable
timeoutsocket.SetDefaultSocketTimeout(20)

However, is there another way to flush the receive buffer so I don't have to
waste 3 seconds waiting for a possibly non-existent banner?

thx









More information about the Python-list mailing list