This question is not limited to Python. Its a general socket question. I have a non-blocking socket and want to connect to a machine which is reachable - on the other side the port does not exist. Why does select(...) succeed anyway? I expected a timeout. sock.send(...) fails with a broken pipe. How can I determine if the socket is really connected after select(...)? Thanks in advance.
import socket, errno, os, time, select
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setblocking(0)
err = sock.connect_ex(('192.168.178.21', 12345))
ready_to_read, ready_to_write, in_error = select.select([], [sock], [], timeout=5)
#ready_to_write is set even 192.168.178.21:12345 does not exist.
sock.setblocking(1)
sock.send('foo') #this fails
sock.close()
Try checking the return values. Here's what I get in a similar situation:
The
select()
call is probably returning because there was an exceptional condition on the socket - that is, its connection was refused.If I do this, the
select()
returns immediately:And interestingly enough, the return values match exactly what you passed in (if you had more than one socket, that would likely be different):
The designers of
select()
are expecting you to runselect()
in a loop, and if a socket returns an error when you attempt the write, remove it from the list when the write fails.So you have a couple of options, off the top of my head:
getsockopt()
as I did above to check if the socket is OK, or is in an error state, before attempting to do anything with it.