[Tutor] Validating String contains IP address
Martin A. Brown
martin at linux-ip.net
Mon Apr 3 19:04:46 EDT 2017
Hello there,
>what am I going wrong here? i need to validate this is an IP or ask
>the question again
>
>untrust_ip_address = raw_input('\nEnter the untrust IP ''"Example 172.20.2.3/28"'':')
This is a representation of an IP address along with the mask length for the
prefix:
172.20.2.3/28
That is not, strictly speaking an IP, because there is ancillary
information included. This corresponds to:
172.20.2.3 IP address
172.20.2.0/28 prefix
The form you are showing is found on some systems, for example in
the output from `ip address show` on Linux systems, but it is a
commonly understood form. Look further at the library that I
recommended last week, and I think you will find a solution.
>while not ipaddress.ip_network untrust_ip_address:
> untrust_ip_address = raw_input('\nEnter the untrust IP ''"Example 172.20.2.3/28"'':')
You might try using the ipaddress library in the following way:
>>> i = ipaddress.ip_interface(u'172.20.2.3/28')
>>> i.ip
IPv4Address(u'172.20.2.3')
>>> i.network
IPv4Network(u'172.20.2.0/28')
>>> i.with_prefixlen
u'172.20.2.3/28'
Good luck,
-Martin
--
Martin A. Brown
http://linux-ip.net/
More information about the Tutor
mailing list