Issue
Im using the asyncio streams API to create a socket connection. Is there a way I can set options such as keep alive setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) ?
I see from the docs that asyncio.open_connection
transfers ownership of the socket over to the streamwrite and the socket object can be closed after, Im unclear if those options get ported over as well.
Am I missing out on something obvious ?
Solution
The socket is available through asyncio.StreamWriter.get_extra_info. You can set socket options using socket.setsockopt from there. Something like:
reader, writer = await asyncio.open_connection(host=host, port=port)
sock = writer.get_extra_info("socket")
sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, True)
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, idle)
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, interval)
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, count)
Answered By - Dave
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.