[Tutor] Checking the format of IP address...

Guillermo Fernandez Castellanos guillermo.fernandez.castellanos at gmail.com
Fri Nov 3 12:18:58 CET 2006


Hi,

I would probably take the string, separate the numbers and check they
are acceptable:

def correct_ip(ip):
   # Check if my IP address has 4 numbers separated by dots
   num=ip.split('.')
   if not len(num)==4:
       return False
   # Check each of the 4 numbers is between 0 and 255
   for n in num:
       try:
           if int(n) < 0 or int(n) > 255:
               return False
       except:
           return False
   return True

You could also go the regexp way, but I find it a bit more complicated.

Hope it helps,

G

On 11/3/06, Asrarahmed Kadri <ajkadri at googlemail.com> wrote:
>
> Hi Folks,
>
> I want to implement a simple program that verifies the IP provided by the
> user is in the right format or not.
>
> How to go about it..?
> Any suggestions..
>
> TIA.
> Regards,
> Asrarahmed
>
>
> --
> To HIM you shall return.
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
>


More information about the Tutor mailing list