How do I find my local ip address?

Asked 1 years ago, Updated 1 years ago, 49 views

Regardless of platform, use standard library Please tell me how to find the local ip address (192.168.x.x or 10.0.x.x)

python ip-address network

2022-09-21 23:11

1 Answers

The modules associated with the network are socket

socket.gethostbyname(hostname) translates hostname to an IPv4 address (string).

socket.getfqdn(name) will receive name and return it to the domain name. (However, if name is empty, interpret it as a local host)

import socket
socket.gethostbyname(socket.getfqdn())

If you write it like this socket.getfqdn returns the local domain name gethostbyname takes this domain name and returns the IPv4 address.


2022-09-21 23:11

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.