I have a service that uses a blocking socket to receive data. The problem I have is that I do not know how to properly close the socket if it is still waiting for data. Below is a short snip of how I am opening and waiting for data: I do not want to implement timeouts as according to the python documentation the socket must be blocking in order to use makefile
.
I may be going about this completely wrong as I am new to programming with sockets.
EDIT:
It should be noted that the I cannot alter how the server operates.
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
reader = s.makefile("rb")
line = reader.readline()
There is a quite simple solution. Given a socket and a reader file object like rfile = socket.makefile('rb')
, in order to make rfile.readline()
return immediately when the socket is closed, you need to execute the following in a separate thread:
socket.shutdown(socket.SHUT_RDWR)
socket.close()
Calling socket.shutdown()
will make rfile.readline()
return an empty str
or bytes
, depending on the version of Python (2.x or 3.x respectively).
One solution to close this socket is to ask the server to close it.
If server close the client socket, the client will receive a "Connection reset by peer" error, and it may break the blocking receive.
Another solution would be to not use readline()
and put a timeout on your socket (indefinite wait could wait.... indefinitely)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With