Python3:如何关闭netlink套接字阻塞recv?

2024-06-16 11:45:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我有如下Python中的原始套接字,用于从linux内核接收netlink消息

socket.socket(socket.AF_NETLINK, socket.SOCK_RAW, socket.NETLINK_ROUTE)

我正在这个套接字上阻塞recv,并希望从另一个Python线程关闭它。但是,在此套接字上调用shutdown(socket.SHUT_RD)会返回一个错误([Errno95] Operation not supported

为什么呢?我们怎样才能关上这个插座?我正在使用Python 3.7

在本书中没有提到这种行为 https://docs.python.org/3/library/socket.html#socket

它规定如下:

Note close() releases the resource associated with a connection but does not necessarily close the connection immediately. If you want to close the connection in a timely fashion, call shutdown() before close().


Tags: the消息closerawlinuxnotsocketconnection
1条回答
网友
1楼 · 发布于 2024-06-16 11:45:30

If you want to close the connection in a timely fashion, call shutdown() before close()

此语句是关于连接的套接字的。TCP套接字已连接,但UDP、Raw、Netlink套接字等未连接。这就是为什么在这样的套接字上不支持shutdown。改用简单的close

相关问题 更多 >