Python gets the IP and physical address of the machine

a code

  1. import socket
  2. import uuid
  3. ip = socket.gethostbyname(socket.gethostname())
  4. node = uuid.getnode()
  5. macHex = uuid.UUID(int=node).hex[-12:]
  6. mac =[]
  7. for i in range(len(macHex))[::2]:
  8. mac.append(macHex[i:i+2])
  9. mac =':'.join(mac)
  10. print('IP:', ip)
  11. print('MAC:', mac)
Second run results
py ========
IP: 192.168.0.105
MAC: 74:df:bf:54:d2:a8
>>> 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327102652&siteId=291194637