[Tutor] Validating String contains IP address
Ed Manning
ejmmanning at gmail.com
Mon Apr 3 19:10:31 EDT 2017
Thank you very much
Sent from my iPhone
> On Apr 3, 2017, at 7:04 PM, Martin A. Brown <martin at linux-ip.net> wrote:
>
>
> 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