2

在 Python 里 disconnect UDP 套接字

 3 years ago
source link: https://blog.lilydjwg.me/2015/4/15/disconnect-udp-sockets-in-python.88294.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

在 Python 里 disconnect UDP 套接字

本文来自依云's Blog,转载请注明。

UDP 套接字是可以使用 connect 系统调用连接到指定的地址的。从此以后,这个套接字只会接收来自这个地址的数据,而且可以使用 send 系统调用直接发数据而不用指定地址。可以再次调用 connect 来连接到别的地方。但是在 Python 里,一旦调用 connect 之后,就再也回不到最初的能够接收从任意地址来的数据的状态了!

这是 Python 的 API 限制,没办法给 connect 方法传递到 AF_UNSPEC 地址簇(在 C 代码里写死了的)。C 里边就可以做到的(代码来自这里):

int disconnect_udp_sock(int fd) {
struct sockaddr_in sin;       
memset((char *)&sin, 0, sizeof(sin));
sin.sin_family = AF_UNSPEC;
return (connect(fd, (struct sockaddr *)&sin, sizeof(sin)));
}

不过既然是 Python 的限制,拿 ctypes 就可以绕过了嘛,有些麻烦就是了:

from ctypes import CDLL, create_string_buffer
def disconnect(sock):
libc = CDLL("libc.so.6")
buf = create_string_buffer(16) # sizeof struct sockaddr_in
libc.connect(sock.fileno(), buf, 16)

AF_UNSPEC 的值是 0,所以把一个和 struct sockaddr_in 一样长的全零缓冲区传给 connect 就可以了 :-)

发送到 Kindle

Category: python | Tags: Python linux 网络 | Read Count: 5180

评论 (0)

[取消回复评论]

昵称 登录 E-mail: *
Web:
Twitter:
当有新评论通过 E-mail 通知我

loading captcha image...
(输入验证码)

or Ctrl+Enter


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK