Python socket gethostbyname() returns only one IP address

徘徊边缘 提交于 2021-01-27 11:50:28

问题


Friends, 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 it's showing that way? is there any other way I can get multiple IP addresses? Please guide me.

Thanks in Advance!

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.

Thanks to All


回答1:


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.


来源:https://stackoverflow.com/questions/43672468/python-socket-gethostbyname-returns-only-one-ip-address

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!