Issue
I am working on python socket programming using asyncio, where I have below code:
buff = b""
try:
while len(buff) < 100):
buff += await loop.sock_recv(srv_sock, 4096)
except Exception as exp:
raise exp
But I am not sure if loop.sock_recv raise any exception.
I was reading about the decription of this API but it doesn't mention that it raises any exception https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.loop.sock_recv
Receive up to nbytes from sock. Asynchronous version of socket.recv().
Return the received data as a bytes object.
sock must be a non-blocking socket.
https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.loop.sock_sendall
This method continues to send to the socket until either all data in data has been sent or an error occurs. None is returned on success. On error, an exception is raised. Additionally, there is no way to determine how much data, if any, was successfully processed by the receiving end of the connection.
Where as it mentions about exception for loop.sock_sendall(sock, data)
Solution
it's most likely not written in the documentation because it can be one of many exceptions.
- it will raise a
ValueError
if the socket is blocking. - a
ConnectionResetError
if the socket is closed abruptly by the OS. (but not when the socket is closed by the other side or on timeouts).
other socket errors may be raised, so if you want to catch them all you should catch ConnectionError which all socket errors should subclass.
Answered By - Ahmed AEK
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.