One of the ways to identify a public IP address is to translate it to an associated host or domain name. This could be done by performing reverse IP lookup to the DNS servers.
The socket module provides IP-related functionalities in Python with gethostbyaddr function allows you to get the host name of an IP address.
$ ipython3 Python 3.8.2 (default, Apr 27 2020, 15:53:34) Type 'copyright', 'credits' or 'license' for more information IPython 7.13.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: import socket
In [2]: socket.gethostbyaddr('93.184.216.34') Out[2]: ('www.example.com', [], ['93.184.216.34'])
Hostname in this context refers to Fully Qualified Domain Name or FQDN.
#!/usr/bin/env python3 import socket import sys address = sys.argv[1] host = socket.gethostbyaddr(address) print('Address: ', address, '\n' 'Host: ', host)
$ python3 ip-to-hostname.py 216.58.196.4 Address: 216.58.196.4 Host: ('kul08s09-in-f4.1e100.net', [], ['216.58.196.4'])
Comment anonymously. Login not required.