ffl*_*ing 2 python sockets macos ioctl system-calls
我正在尝试在Mac上运行以下功能,它正在投掷
struct.pack('iL', bytes, names.buffer_info()[0])
IOError: [Errno 102] Operation not supported on socket
Run Code Online (Sandbox Code Playgroud)
它在linux上运行得很好.谁能告诉我发生了什么事?
码:
def _get_interface_list():
max_iface = 32 # Maximum number of interfaces(Aribtrary)
bytes = max_iface * 32
is_32bit = (8 * struct.calcsize("P")) == 32 # Set Architecture
struct_size = 32 if is_32bit else 40
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
names = array.array('B', '\0' * bytes)
outbytes = struct.unpack('iL', fcntl.ioctl(
s.fileno(),
0x8912, # SIOCGIFCONF
struct.pack('iL', bytes, names.buffer_info()[0])
))[0]
namestr = names.tostring()
return namestr
Run Code Online (Sandbox Code Playgroud)
问题是Mac OS X和其他BSD系统不支持SIOCGIFHWADDR
.您将不得不使用getifaddrs,现在Linux也支持它,尽管它似乎没有被Python公开.但是,您可以使用ctypes来实现此目的.我希望这个例子(BSD风格的许可证)可以帮助你.
此外,您可以通过使用netifaces来避免所有麻烦.
归档时间: |
|
查看次数: |
1912 次 |
最近记录: |