By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,626 Members | 1,146 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,626 IT Pros & Developers. It's quick & easy.

How to validate IP address in Python

P: 17
Hi,

My programme has a text box where in user enters the ip address. I need to validate the IP (like no space allowed in the entry, the numerals should not exceed 255, not more than 3 dots allowed overall, no consecutive dots allowed etc.,.). How do i go about the same? Is there any built in function for the same?

thanks,
Badri
Nov 27 '06 #1
Share this Question
Share on Google+
5 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
Hi,

My programme has a text box where in user enters the ip address. I need to validate the IP (like no space allowed in the entry, the numerals should not exceed 255, not more than 3 dots allowed overall, no consecutive dots allowed etc.,.). How do i go about the same? Is there any built in function for the same?

thanks,
Badri
Badri,

This may cover most of of your requirements:
Expand|Select|Wrap|Line Numbers
  1. ip_str = "23.34.45.45"
  2.  
  3. def ipFormatChk(ip_str):
  4.     if len(ip_str.split()) == 1:
  5.         ipList = ip_str.split('.')
  6.         if len(ipList) == 4:
  7.             for i, item in enumerate(ipList):
  8.                 try:
  9.                     ipList[i] = int(item)
  10.                 except:
  11.                     return False
  12.                 if not isinstance(ipList[i], int):
  13.                     return False
  14.             if max(ipList) < 256:
  15.                 return True
  16.             else:
  17.                 return False
  18.         else:
  19.             return False
  20.     else:
  21.         return False
  22.  
  23. print ipFormatChk(ip_str)
  24. True
Nov 27 '06 #2

bartonc
Expert 5K+
P: 6,596
Nice coding, BV!

Badri,

This may cover most of of your requirements:
Expand|Select|Wrap|Line Numbers
  1. ip_str = "23.34.45.45"
  2.  
  3. def ipFormatChk(ip_str):
  4.     if len(ip_str.split()) == 1:
  5.         ipList = ip_str.split('.')
  6.         if len(ipList) == 4:
  7.             for i, item in enumerate(ipList):
  8.                 try:
  9.                     ipList[i] = int(item)
  10.                 except:
  11.                     return False
  12.                 if not isinstance(ipList[i], int):
  13.                     return False
  14.             if max(ipList) < 256:
  15.                 return True
  16.             else:
  17.                 return False
  18.         else:
  19.             return False
  20.     else:
  21.         return False
  22.  
  23. print ipFormatChk(ip_str)
  24. True
Nov 27 '06 #3

P: 1
You can also use regular expressions like this
Expand|Select|Wrap|Line Numbers
  1. import re
  2. ip_str = "23.34.45.45"
  3.  
  4. def ipFormatChk(ip_str):
  5.    pattern = r"\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b"
  6.    if re.match(pattern, ip_str):
  7.       return True
  8.    else:
  9.       return False
  10. print ipFormatChk(ip_str)
  11.  
Dec 1 '06 #4

bvdet
Expert Mod 2.5K+
P: 2,851
You can also use regular expressions like this
Expand|Select|Wrap|Line Numbers
  1. import re
  2. ip_str = "23.34.45.45"
  3.  
  4. def ipFormatChk(ip_str):
  5.    pattern = r"\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b"
  6.    if re.match(pattern, ip_str):
  7.       return True
  8.    else:
  9.       return False
  10. print ipFormatChk(ip_str)
  11.  
Regular expressions are ideal for this application. This code executes much faster than the other. The drawback is readability. An alternative is to set the VERBOSE flag and add comments:
Expand|Select|Wrap|Line Numbers
  1. pattern = re.compile(r"""
  2.    \b                                           # matches the beginning of the string
  3.    (25[0-5]|                                    # matches the integer range 250-255 OR
  4.    2[0-4][0-9]|                                 # matches the integer range 200-249 OR
  5.    [01]?[0-9][0-9]?)                            # matches any other combination of 1-3 digits below 200
  6.    \.                                           # matches '.'
  7.    (25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)       # repeat
  8.    \.                                           # matches '.'
  9.    (25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)       # repeat
  10.    \.                                           # matches '.'
  11.    (25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)       # repeat
  12.    \b                                           # matches the end of the string
  13.    """, re.VERBOSE)
It executes slower, but I guess you can't have everything! Thanks for posting Mike.
Dec 1 '06 #5

bartonc
Expert 5K+
P: 6,596
Regular expressions are ideal for this application. This code executes much faster than the other. The drawback is readability. An alternative is to set the VERBOSE flag and add comments:
Expand|Select|Wrap|Line Numbers
  1. pattern = re.compile(r"""
  2.    \b                                           # matches the beginning of the string
  3.    (25[0-5]|                                    # matches the integer range 250-255 OR
  4.    2[0-4][0-9]|                                 # matches the integer range 200-249 OR
  5.    [01]?[0-9][0-9]?)                            # matches any other combination of 1-3 digits below 200
  6.    \.                                           # matches '.'
  7.    (25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)       # repeat
  8.    \.                                           # matches '.'
  9.    (25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)       # repeat
  10.    \.                                           # matches '.'
  11.    (25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)       # repeat
  12.    \b                                           # matches the end of the string
  13.    """, re.VERBOSE)
It executes slower, but I guess you can't have everything! Thanks for posting Mike.
I like you style, BV.
Dec 1 '06 #6

Post your reply

Sign in to post your reply or Sign up for a free account.