One of the ways to identify a public IP address is to translate it to an associated host name.
IP-related functions in Python are provided by the socket module and gethostbyaddr function within the module 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 : import socket
In : socket.gethostbyaddr('220.127.116.11') Out: ('www.example.com', , ['18.104.22.168'])
Hostname in this context refers to Fully Qualified Domain Name or FQDN.
#!/usr/bin/env python3 import socket import sys address = sys.argv host = socket.gethostbyaddr(address) print('Address: ', address, '\n' 'Host: ', host)
$ python3 ip-to-hostname.py 22.214.171.124 Address: 126.96.36.199 Host: ('kul08s09-in-f4.1e100.net', , ['188.8.131.52'])
Comment anonymously. Login not required.