How to Get IP Addresses in Python?

In a computer network, IP addresses are numerical labels assigned to devices. IP addresses are vital for identifying devices and facilitating communication in computer networks. The Internet uses the IP to identify and locate devices. To get IP addresses, various functions are used in Python.

This post provides multiple ways to get an IP address in Python using appropriate examples. The following contents will get you started:

  • Method 1: Using gethostname() and gethostbyname() Functions
  • Method 2: Using the Requests Module
  • Method 3: Using the socket.getaddrinfo()
  • Method 4: Using the socket.getsockname() Function
  • Method 5: Using the socket.gethostbyname_ex() Function

Method 1: Using gethostname() and gethostbyname() Functions

The “gethostname()” function returns the standard host name for the local computer. In contrast, “gethostbyname()” returns host information from a host database corresponding to a hostname. The below code is used to get an IP address using this method:

Code:

import socket
hostname = socket.gethostname()
ip_address = socket.gethostbyname(hostname)
print(f"Hostname: {hostname}")
print(f"IP Address: {ip_address}")
  • The “socket.gethostname()” function is utilized to get the hostname of the computer.
  • The “socket.gethostbyname()” function accepts the hostname and retrieves the IP address of the computer.

Output:

The hostname and IP address have been returned.

Method 2: Using the Requests Module

In Python, you can send HTTP requests by using the requests module. The below code uses the requests module functions to get IP addresses:

Code:

import requests
response = requests.get('https://api.ipify.org')
ip_address = response.text
print(f'Your IP Address is {ip_address}')
  • The “requests.get()” function is used to send an HTTP GET request to the specified URL “https://api.ipify.org”.
  • The IP address is extracted from the response using the “.text” attribute.

Output:

The IP address has been displayed.

Method 3: Using the socket.getaddrinfo()

The “socket.getaddrinfo()” function is used to perform a domain name resolution. The below code uses the “socket.getaddrinfo()” function to obtain the IP address in Python:

Code:

import socket
hostname = "www.google.com"
addr_info = socket.getaddrinfo(hostname, 80)
for info in addr_info:
    print("IP Address:", info[4][0])
  • The hostname “www.google.com” is assigned to a variable..
  • The “getaddrinfo()” function is used to find an IP address for a given hostname and port (80 is the default HTTP port). 
  • This function returns a list of tuples, with each tuple containing information about a particular address (such as the address family, socket type, protocol, and address itself).
  • A for loop is utilized to loop through the list of address tuples.
  • The print() function prints the IP address (which is the fourth element of each address tuple) for each address. 

Output:

The IP address (IPv6 and IPv4) has been displayed.

Method 4: Using the socket.getsockname() Function

The “getsockname()” function returns a tuple that contains the local IP address and port number to which the socket is bound. The below example uses this function to get an IP address in Python:

Code:

import socket
my_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
my_socket.connect(("8.8.8.8", 80))
print('Your IP Address is',my_socket.getsockname()[0])
  • The “socket.socket()” function accepts two arguments “socket.AF_INET” and “socket.SOCK_DGRAM” and creates a socket object.
  • The first argument “socket.AF_INET” specifies that we are using IPv4 and the second argument “socket.SOCK_DGRAM” specifies that we are using UDP protocol.
  • The connect() method is used to connect the socket to the IP address and port number specified in the tuple (“8.8.8.8”, 80).
  • The “getsockname()” method returns a tuple that contains the IP address as the first element.

Output:

The IP address has been returned.

Method 5: Using the socket.gethostbyname_ex() Function

This Python function returns the IP address(es) of a given hostname, along with alias names for the hostname and IP addresses for all network interfaces. The below code is used to get an IP address in Python:

Code:

import socket
hostname = "www.google.com"
name, aliaslist, addresslist = socket.gethostbyname_ex(hostname)
print("Hostname:", name)
for address in addresslist:
    print("IP Address:", address)
  • The “gethostbyname_ex()” method takes the hostname as an argument and returns a tuple containing three values: the primary hostname, a list of alias names for the hostname, and a list of IP addresses for all network interfaces on the system that match the hostname.
  • These three values are then unpacked into the variables name, aliaslist, and addresslist, respectively.
  • The primary hostname is returned by using the name variable and the for loop is used to loop through each IP address in the addresslist variable.

Output:

The IP address of the specified Hostname has been displayed.

Conclusion

The “gethostbyname()” function, “requests module”, “socket.getaddrinfo()”, and “socket.getsockname()” functions are used to get IP addresses in Python. The “gethostname()” and “gethostbyname()” functions of the socket module are used to get the hostname and IP addresses. Similarly the “request modules” also provide functions that can get the IP address. The other methods can also be used to obtain the IP address of the particular hostname. This guide presented various ways to obtain the IP address in Python using numerous examples.