DJay
DJay

Reputation: 25

gethostbyaddr gives a different host name

My gethostbyaddr() is giving a different host name from what I need. The host name that I need does not even show up in the alias list.

So, I was confused with the weird hostnames that I get when i try gethostbyaddr for different websites. So, I tried getting the ip of amazon using gethostbyname. Then I used the resulting IP in gethostbyaddr() but I did not get the hostname of amazon. I read the official documentation and it states that the alias list returned contains the alternative host names, but I still do not get www.amazon.com

So This is what I tried doing. socket.gethostbyname('www.amazon.com') And my result was: '13.35.134.162' Then I input this IP: socket.gethostbyaddr('13.35.134.162') But my result is: ('server-13-35-134-162.del54.r.cloudfront.net', [], ['13.35.134.162'])

Can someone explain why 'www.amazon.com' is not displayed and what is this hostname that I get?

Upvotes: 0

Views: 944

Answers (3)

VPfB
VPfB

Reputation: 17237

A website name is not equal to hostname. There is no 1:1 relationship in general. One computer can serve many websites. OTOH a busy website is served by many computers (so called load balancing). CDNs (content delivery networks) use some BGP-4 tricks (BGP-4 = an important routing protocol) to connect you to a server geographically near you - they run several "clones" of a webiste in different locations.

What are your needs? If you want to be sure you are connected to the right website, rely on HTTPS certificates.

Upvotes: 1

user3097732
user3097732

Reputation: 159

This has nothing to do with python, but rather how DNS works. A single IP address can host many web sites and thus have many host names. As a result, the name to IP lookup is not always reversible.

Upvotes: 1

kicken
kicken

Reputation: 2167

The name you get back, if any at all, is dependent on how the network administrator setup their reverse dns.

If the reverse dns is setup, in my experience it is usually set to something more technical or dynamic rather than a simple name. The reason for this usually has to do with the fact that many sites may share an IP address and/or a site resolves to many IPs.

As such, instead of trying to reverse the IP to one of those site names, instead you'd reverse it to something that identifies that particular server.

Upvotes: 2

Related Questions