ffledgling
ffledgling

Reputation: 12130

Python ioctl error on Mac OS

I'm trying to run the following function on a Mac and It's throwing

struct.pack('iL', bytes, names.buffer_info()[0])
IOError: [Errno 102] Operation not supported on socket

It works just fine on linux. Can anyone tell me what's going on?


Code:

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

Upvotes: 2

Views: 2844

Answers (2)

David Bern
David Bern

Reputation: 776

Flag value of SIOCGIFCONF is 0xc00c6924 on OS X 10.9.

updated

I did some minor testing and it didn't throw any errors. Im not sure if it 100% working or not but I also found this from Apple: https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man4/ip.4.html where "addr" is the local IP address of the desired interface or INADDR_ANY to specify the default interface. An interface's local IP address and multicast capability can be obtained via the SIOCGIFCONF and SIOCGIFFLAGS ioctls. Normal applications should not need to use this option.

Upvotes: 0

jweyrich
jweyrich

Reputation: 32240

The problem is that Mac OS X and other BSD systems don't support SIOCGIFHWADDR. You'll have to use getifaddrs, which is now also supported by Linux, though it does not seem to be exposed by Python. However, you can use ctypes to accomplish that. I hope this example (BSD-style license) helps you.

Futhermore, you could simply avoid all the trouble by using netifaces.

Upvotes: 5

Related Questions