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('18.104.22.168') Out: ('www.example.com', , ['22.214.171.124'])
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 126.96.36.199 Address: 188.8.131.52 Host: ('kul08s09-in-f4.1e100.net', , ['184.108.40.206'])
Comment anonymously. Login not required.