Python gets the native IP address code sharing under Linux
- 2020-04-02 14:19:52
- OfStack
Sometimes when it comes to getting a native IP, the following is used.
#!/usr/bin/python
import socket
import struct
import fcntl
def getip(ethname):
s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(s.fileno(), 0X8915, struct.pack( ' 256s', ethname[:15]))[20:24])
if __name__=='__main__':
print getip('eth0')