[Tutor] Validating String contains IP address
What's the best way to validate a string contains a IP address Sent from my iPad ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Validating String contains IP address
Hello 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"'':') while not ipaddress.ip_network untrust_ip_address: untrust_ip_address = raw_input('\nEnter the untrust IP ''"Example 172.20.2.3/28"'':') > On Apr 1, 2017, at 12:29 PM, Alex Kleider wrote: > > On 2017-03-31 18:01, Mats Wichmann wrote: >> On 03/31/2017 06:44 PM, Alex Kleider wrote: >>> On 2017-03-31 16:35, Ed Manning wrote: >>>> What's the best way to validate a string contains a IP address >>>> Sent from my iPad >>>> ___ >>>> Tutor maillist - Tutor@python.org >>>> To unsubscribe or change subscription options: >>>> https://mail.python.org/mailman/listinfo/tutor >>> The re module perhaps? > >> This assumes "an IP address" is the four dotted numbers characteristic >> of IPv4. These days that's a bad assumption unless you're absolutely >> sure an IPv6 address can never appear. Can you? > > Good point! I hadn't considered IPV6 and didn't know about the ipaddress > module. > Live and learn. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Validating String contains IP address
Thank you very much Sent from my iPhone > On Apr 3, 2017, at 7:04 PM, Martin A. Brown 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/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor