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
Steps to get host name from IP address in Python
$ 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('188.8.131.52') Out: ('www.example.com', , ['184.108.40.206'])
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 220.127.116.11 Address: 18.104.22.168 Host: ('kul08s09-in-f4.1e100.net', , ['22.214.171.124'])
Comment anonymously. Login not required.