Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding ip address using python's socket module

When I run the following command to get the host ip:

socket.gethostbyname(socket.gethostname())

I am getting the following error on MAC terminal:

socket.gaierror: [Errno 8] nodename nor servname provided, or not known

Any idea what the problem could be?

socket.gethostname() does return the correct hostname.

like image 509
KT100 Avatar asked Apr 15 '26 15:04

KT100


1 Answers

Try to open as root /etc/hosts file, and add a line like the following

# IP ADDR      HOSTNAME
192.168.1.1    your_desired_hostname

and the name should resolve.

Obviously substitute 192.168.1.1 with your ip address.

like image 153
dave Avatar answered Apr 17 '26 04:04

dave



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!