Obtain the IP address of the specified network card through Python under Linux

From , 3 Years ago, written in Python, viewed 218 times.
URL https://pastebin.vip/view/80f24ef4
  1. import socket
  2. import fcntl
  3. import struct
  4.  
  5. def get_ip_address(ifname):
  6.    """
  7.    >>> get_ip_address('lo')
  8.    '127.0.0.1'
  9.  
  10.    >>> get_ip_address('eth0')
  11.    '38.113.228.130'
  12.    """
  13.     s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  14.     return socket.inet_ntoa(fcntl.ioctl(
  15.         s.fileno(),
  16.         0x8915,  # SIOCGIFADDR
  17.         struct.pack('256s', ifname[:15])
  18.     )[20:24])
  19. #//python/8191

Reply to "Obtain the IP address of the specified network card through Python under Linux"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website