Last modified: Sep 13, 2026

Check If IP Address Is In Subnet Using Python

When working with networks, developers often need to determine whether an IP address belongs to a specific subnet. Python provides powerful tools to handle this task efficiently. This article explains how to check if an IP address is in a subnet using Python, covering both IPv4 and IPv6.

Understanding IP Addresses and Subnets

An IP address identifies a device on a network. A subnet (short for subnetwork) divides a larger network into smaller segments. Each subnet has a range of valid IP addresses.

Python simplifies network-related tasks through its ipaddress module. This module allows developers to create, manipulate, and inspect IP addresses and networks.

Using the ipaddress Module

The ipaddress module is part of Python's standard library. It supports both IPv4 and IPv6 addresses and networks. To use it, import the module at the start of your script:


import ipaddress

Checking IPv4 Subnets

To check if an IPv4 address belongs to a subnet, follow these steps:

  1. Create an IPv4Address object for the IP address.
  2. Create an IPv4Network object for the subnet.
  3. Use the in operator to test membership.

Here’s an example:


import ipaddress

# Define the IP address and subnet
ip = ipaddress.IPv4Address('192.168.1.5')
network = ipaddress.IPv4Network('192.168.1.0/24')

# Check if the IP is in the subnet
is_in_subnet = ip in network
print(is_in_subnet)

Output:


True

This output confirms that 192.168.1.5 is within the 192.168.1.0/24 subnet.

Checking IPv6 Subnets

The same approach works for IPv6 addresses:


import ipaddress

# Define the IPv6 address and subnet
ip = ipaddress.IPv6Address('2001:db8::1')
network = ipaddress.IPv6Network('2001:db8::/32')

# Check if the IP is in the subnet
is_in_subnet = ip in network
print(is_in_subnet)

Output:


True

Handling Invalid Inputs Gracefully

Invalid IP addresses or subnets can cause exceptions. Wrap your code in a try-except block to handle errors:


import ipaddress

def check_ip_in_subnet(ip_str, subnet_str):
    try:
        ip = ipaddress.ip_address(ip_str)
        network = ipaddress.ip_network(subnet_str)
        return ip in network
    except ValueError as e:
        print(f"Error: {e}")
        return False

# Example usage
result = check_ip_in_subnet('999.999.999.999', '192.168.1.0/24')
print(result)

Output:


Error: '999.999.999.999' does not appear to be an IPv4 or IPv6 address
False

Comparing Multiple IP Addresses

You can check multiple IP addresses against a subnet using a loop:


import ipaddress

# Define subnet and list of IPs
network = ipaddress.IPv4Network('192.168.1.0/24')
ips_to_check = ['192.168.1.1', '192.168.2.1', '192.168.1.255']

# Check each IP
for ip_str in ips_to_check:
    ip = ipaddress.IPv4Address(ip_str)
    print(f"{ip_str} in subnet: {ip in network}")

Output:


192.168.1.1 in subnet: True
192.168.2.1 in subnet: False
192.168.1.255 in subnet: True

Using Strict Mode for Networks

By default, the ip_network function uses strict mode. It raises an error if the network address has host bits set. To allow non-strict parsing, pass strict=False:


import ipaddress

# Non-strict parsing
network = ipaddress.ip_network('192.168.1.100/24', strict=False)
print(network)

Output:


192.168.1.0/24

Conclusion

Python’s ipaddress module makes checking if an IP address is in a subnet simple and reliable. Whether working with IPv4 or IPv6, the same logic applies. By handling exceptions and using loops, you can build robust network utilities.

Mastering these techniques is essential for developers working with network configurations, security tools, or automation scripts. Practice with real-world examples to strengthen your understanding.