How to use python to get Network Card and ip
This article mainly explains "how to use python to obtain network card and ip", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "how to use python to obtain network card and ip"!
Def get_interface_ip (ifname): sckt = socket.socket (socket.AF_INET, socket.SOCK_DGRAM) return socket.inet_ntoa (fcntl.ioctl (sckt.fileno (), 0x8915, # SIOCGIFADDR struct.pack ('256s') Ifname [: 15]) [20:24]) def _ get_interface_list (): "Provides a list of available network interfaces as a list of tuples (name) Ip) "" 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 try: s = socket.socket (socket.AF_INET, socket.SOCK_DGRAM) names = array.array ('baked,'\ 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 [i0x8912 + 32] .split ('\ 0mm, 1) [0]) Socket.inet_ntoa (namestre [I + 20 struct_size I + 24]))\ for i in range (0, outbytes, struct_size)] except IOError: raise NetworkError ('Unable to call ioctl with SIOCGIFCONF') so far I believe you have a deeper understanding of "how to use python to obtain network card and ip". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!