Ams
Ams

Reputation: 1

How to find a host's FQDN from its IP address in Python

I am writing a python script in which I have an IP address from which I need to find the associated host's FQDN.

I know there are socket.getfqdn() and socket.gethostbyaddr() calls but they are giving only the hostname in this case and not the FQDN.

However, if I'm running 'host ' or 'nslookup ' on my local linux pc it shows me the FQDN of that host.

Please help me if there is any way to achieve it in Python.

Upvotes: 0

Views: 2220

Answers (1)

FrobberOfBits
FrobberOfBits

Reputation: 18022

Use dnspython.

Examples for reverse lookup include things like this:

>>> from dns import resolver,reversename
>>> addr=reversename.from_address("74.125.228.97")
>>> str(resolver.query(addr,"PTR")[0])
'iad23s08-in-f1.1e100.net.'
>>> 

Upvotes: 1

Related Questions