On Sun, Feb 18, 2018 at 4:35 AM, Wildman via Python-list <python-list@python.org> wrote: > Thanks to Chris and Ben. Your suggestions were slightly > different but both worked equally well, although I don't > understand how that can be so. > >> struct.pack('256s', ifname[:15].encode('ascii')) >> struct.pack('256s', ifname.encode('ascii'))
Those two will be identical for short interface names, but the first form will truncate a longer name before encoding. I don't know what the ioctl will do with a really long ifname, so it's probably worth hanging onto the [:15] slicing. > I was looking for a reliable way to determine the IP addy > for a given network adapter. That is what the code does > but I don't understand how it does it either. > > Thanks again. I will continue to research and study the > code in the hope I will understand it. Ah, makes sense. I'd probably do something like this: >>> import socket >>> s = socket.socket(type=socket.SOCK_DGRAM) >>> s.connect(("8.8.8.8", 53)) >>> s.getsockname()[0] '192.168.0.19' But that's only going to show one (uplink) address. If I needed to get ALL addresses for ALL network adapters, I'd either look for a library, and if one wasn't easily found, I'd shell out to the "ip" command and parse its output. :) ChrisA -- https://mail.python.org/mailman/listinfo/python-list