Python Gets the native IP, MAC address, computer name
Getting an IP address in Python is a big difference in PHP, so let's take a look at Python's native MAC address:
>>> Import UUID
>>> def get_mac_address ():
Mac = uuid. UUID (int = Uuid.getnode ()). HEX[-12:]
Return ': '. Join ([mac[e:e+2] for E in range (0,11,2)])
>>> get_mac_address ()
' A4:be:6d:99:87:db '
Here's another look at Python's approach to acquiring IP: using the socket
>>> Import Socket
>>> hostname = SOCKET.GETFQDN (Socket.gethostname ())
>>> Print (hostname)
User-20160730zr
>>> hostaddr = socket.gethostbyname (hostname)
>>> Print (HOSTADDR)
192.168.217.1
But note that the IP you get here is the intranet IP
Method Two: Available under Linux, window is invalid
Import socket
Import Fcntl
Import struct
def get_ip_address (ifname):
s = socket.socket (socket.af_inet, socket. SOCK_DGRAM)
Return Socket.inet_ntoa (Fcntl.ioctl (
S.fileno (),
0x8915, # SIOCGIFADDR
Struct.pack (' 256s ', ifname[:15])
) [20:24])
>>> get_ip_address (' lo ')
' 127.0.0.1 '
>>> get_ip_address (' eth0 ')
' 38.113.228.130 '
Get the native IP, MAC address, computer name