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
$ 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
socket.gethostbyaddrto get host name from an
In : socket.gethostbyaddr('184.108.40.206') Out: ('www.example.com', , ['220.127.116.11'])
Hostname in this context refers to
Fully Qualified Domain Name or
Pythonscript that accepts an
IPaddress as parameter and outputs corresponding host information.
#!/usr/bin/env python3 import socket import sys address = sys.argv host = socket.gethostbyaddr(address) print('Address: ', address, '\n' 'Host: ', host)
IPaddress as parameter.
$ python3 ip-to-hostname.py 18.104.22.168 Address: 22.214.171.124 Host: ('kul08s09-in-f4.1e100.net', , ['126.96.36.199'])
Comment anonymously. Login not required.