Search code examples
pythonsocketsipgethostbyname

socket.gethostbyname giving wrong IP


socket.gethostbyname("vidzi.tv") giving '104.20.87.139'

ping vidzi.tv gives '104.20.86.139'

socket.gethostbyname("www.vidzi.tv") giving '104.20.87.139'

ping www.vidzi.tv gives '104.20.86.139'

Why socket.gethostbyname is giving wrong IP for this website? It is giving right IP for other websites?


Solution

  • I don't see any "wrong" IPs in your question. A DNS server is allowed to return multiple IP addresses for the same host. The client generally just picks one of them. A lot of servers use this as a part of their load balancing, as clients select any available server and since they generally would pick different ones the traffic gets split up evenly. Your ping command and your gethostbyname command are just selecting different available IPs, but neither is "wrong".

    You can see all the IPs that are returned for a given hostname with a tool like nslookup or dig.