Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python socket gethostbyname() returns only one IP address

Tags:

python

sockets

I'm doing some simple experiments using Python socket, where I've a HOSTNAME which resolves with two IP addresses but when I use,

socket.gethostbyname('demo.sample.com')

I'm getting only one IP address. Why is it showing that way? Is there any other way I can get multiple IP addresses?

EDIT - 1

I got it guys, instead of gethostbyname('demo.sample.com')
I tried gethostbyname_ex('demo.sample.com')
It gives the result as I expected.

like image 669
Sundararajan Avatar asked Feb 04 '23 13:02

Sundararajan


1 Answers

From the documentation it is visible that:

  • gethostbyname returns only a single IPv4 address. And to cite:
    See gethostbyname_ex() for a more complete interface.
  • gethostbyname_ex will return multiple IPv4 address, but check out the usage. And to cite:
    gethostbyname_ex() does not support IPv6 name resolution, and getaddrinfo() should be used instead for IPv4/v6 dual stack support.
  • getaddrinfo will return all IPv4 and IPv6 addresses, but check out the usage.
like image 166
Steffen Ullrich Avatar answered Feb 08 '23 15:02

Steffen Ullrich