Linux上的Python Socket编程 - Errno 19:没有这样的设备

1 投票
1 回答
5517 浏览
提问于 2025-04-28 06:59

我遇到了一点问题。在调试这段代码的时候:

s=socket(AF_PACKET, SOCK_RAW, IPPROTO_IP)
try:
    host=gethostbyname(gethostname())
    s.bind((host, 0))

    subprocess.check_call(['ifconfig', 'eth0', 'promisc'])

我得到了一个错误 [Errno 19] No such device,这个错误是在调用 s.bind((host, 0)) 这一行时出现的。

暂无标签

1 个回答

1

因为你在使用原始套接字,所以传给 bind 的参数需要相应调整。下面是关于 socket.bind 的帮助文档中的一段代码示例。

bind(...) unbound socket._socketobject method
bind(address)

Bind the socket to a local address.  For IP sockets, the address is a
pair (host, port); the host must refer to the local host. For raw packet
sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])

我猜 s.bind(('eth0', N)) 这个写法应该适合你。你需要了解一下如何选择 N 的正确值。

撰写回答