gethostbyname() Example:
import socket
print(socket.gethostbyname('google.com'))
This Python code performs the following tasks:
-
Imports the 'socket' module using the 'import' keyword. This module provides a way to communicate over a network.
-
Calls the 'socket.gethostbyname()' method, passing the string 'google.com' as an argument. This method returns the IP address of the machine that is associated with the given hostname 'google.com'.
-
Prints the returned IP address to the console using the 'print()' function.
Let's break down each line of code in more detail:
import socket
This line imports the 'socket' module into the script, which provides access to the socket interface for network communication.
print(socket.gethostbyname('google.com'))
This line calls the 'socket.gethostbyname()' method, passing the string 'google.com' as an argument. This method returns the IP address of the machine that is associated with the given hostname 'google.com'. The returned IP address is then printed to the console using the 'print()' function.
Previous code retrieves and displays the IP address of the machine that is associated with the hostname 'google.com'.
gethostrbyaddr() Example
import socket
print(socket.gethostbyaddr('That.IP.Goes.Here'))
This Python code performs the following tasks:
-
Imports the 'socket' module using the 'import' keyword. This module provides a way to communicate over a network.
-
Calls the 'socket.gethostbyaddr()' method, passing a string 'That.IP.Goes.Here' as an argument. This method returns a tuple containing the hostname, alias list, and IP addresses of the machine that is associated with the given IP address 'That.IP.Goes.Here'.
-
Prints the returned hostname to the console using the 'print()' function.
Let's break down each line of code in more detail:
import socket
This line imports the 'socket' module into the script, which provides access to the socket interface for network communication.
print(socket.gethostbyaddr('That.IP.Goes.Here'))
This line calls the 'socket.gethostbyaddr()' method, passing the string 'That.IP.Goes.Here' as an argument. This method returns a tuple containing the hostname, alias list, and IP addresses of the machine that is associated with the given IP address 'That.IP.Goes.Here'. The returned tuple is then printed to the console using the 'print()' function.
The tuple returned by 'socket.gethostbyaddr()' contains the following values:
- The primary hostname for the IP address.
- A list of alternate hostnames for the IP address.
- A list of IP addresses associated with the hostname.
If the IP address provided does not resolve to a valid hostname, then an exception is raised.
This code retrieves and displays the hostname associated with the IP address 'That.IP.Goes.Here'.
No comments:
Post a Comment