Tuesday, May 16, 2023
HomeSoftware Engineeringget the IP Deal with in Python

get the IP Deal with in Python


If you must get the IP Deal with in your Python software, then you are able to do the next:

Possibility 1 – Utilizing socket.gethostname()

import socket
print(socket.gethostbyname(socket.gethostname()))

Possibility 2 – Utilizing socket.getsockname()

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.join(("8.8.8.8", 80))
print(s.getsockname()[0])

Possibility 3 – Utilizing the netifaces module

from netifaces import interfaces, ifaddresses, AF_INET
for ifaceName in interfaces():
    addresses = [i['addr'] for i in ifaddresses(ifaceName).setdefault(AF_INET, [{'addr':'No IP addr'}] )]
    print(' '.be part of(addresses))
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments