struct.error: argument for 's' must be a bytes object in python 3.4

networking, python

Solution

You need to convert the `ifname` string into bytes. You also don't need to call ord(), since ioctl returns bytes, not a string:

...
info = fcntl.ioctl(s.fileno(), 0x8927,  struct.pack('256s', bytes(ifname[:15], 'utf-8')))
return ''.join(['%02x:' % b for b in info[18:24]])[:-1]
...

See this SO question for more info about strings and bytes in python3

Problem

Code I'm attempting to use in python 3.4: ``` #!/usr/bin/python3 def get_mac_addr(ifname): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) info = fcntl.ioctl(s.fileno(), 0x8927, struct.pack('256s', ifname[:15])) return ''.join(['%02x:' % ord(char) for char in info[18:24]])[:-1] print (get_mac_addr('eth0')) Error: struct.error: argument for 's' must be a bytes object ``` I see that this code does work when not using python3 but I need it in 3 for my project. I tried comparing to problem: Struct.Error, Must Be a Bytes Object? but I couldn't see how I can apply this to myself.

Original source

Related problems