# How to Validate IP Address in Python

In this tutorial, we will learn how to validate an IP address using python. So, first of all, we should know about the IP address. Whenever an IP address is given to a computer it is treated as a host. There are basically two types of representation of IP addresses-

• Internet Protocol version 4 (IPv4)
• Internet Protocol version 6 (IPv6)

We will discuss how to validate IPv4 address using python. But we should know the format of the IPv4 address. It is a 32-bits logical address consisting of 4 parts each of 8-bits (decimal value 0-255) separated by ‘`.`‘. The example of a valid IP address is 192.21.56.201 whereas 256.-1.34.6 or 23.45 etc are invalid IP addresses.

So, let’s see how to validate the IP addresses.

## Validate IP address using Python

We can validate an IP address in python using two approaches.

• Using general programming
• Using regular expression

### Validate Ip address using general programming

Algorithm:

• Take the IP address as a string.
• Traverse through the string and check the format of the IP address i.e A.B.C.D where 0<=A, B, C, D<=255
• If the given IP address follows the above format print ‘Valid IP’ else print ‘Invalid IP’.

#### Code:

See the below code.

```def validateIP(ip):
start=0
count=0
flag=0
for i in range(len(ip)):
if(ip[i]=='.'):
count+=1
if(len(ip[start:i])==0 or int(ip[start:i])>255 or int(ip[start:i])<0):
flag=1
break
else:
start=i+1
if(flag==1):
print("Invalid IP")
else:
if(count==3 and len(ip[start:])!=0 and int(ip[start:])<=255 and int(ip[start:])>=0):
print("Valid IP")
else:
print("Invalid Ip")

#Driver Program
ip=input()
validateIP(ip)
```

In the above code, ‘`validateIP`‘ function check whether the given IP is valid or not.

Output:

```Enter Ip address
192.21.56.201
Valid IP```

Another output of the above code:

```Enter Ip address
256.-1.34.6
Invalid IP```

### Validate IP address using a regular expression:

First of all, you should have knowledge about regular expression in python before going forward. So, gather some knowledge about regular expression from Regular expression in python and More about Regular Expression.

#### Algorithm:

• Import the ‘`re`‘ module.
• Write the regular expression for valid IP i.e the format for valid IP A.B.C.D where 0<=A, B, C, D<=255
• Use the ‘`re.search()`‘ method and pass the regular expression and the IP to be checked as a parameter of the function to check whether the IP is a valid one or not.

#### Code:

See the below code.

```import re

def validateIP(ip):
ip_expression = '''^(25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.(
25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.(
25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.(
25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)'''
if(re.search(ip_expression,ip)):
print("Valid IP")
else:
print("Invalid IP")

#driver program

ip=input()
validateIP(ip)

```

Output:

```Enter the IP address
192.21.56.201
Valid IP```

Another output of the ab0ve program:

```Enter the IP address
23.45
Invalid IP```

That’s all. You may also read-