[Tutor] Validating String contains IP address

Steven D'Aprano steve at pearwood.info
Sat Apr 1 00:24:31 EDT 2017


On Fri, Mar 31, 2017 at 07:35:48PM -0400, Ed Manning wrote:
> 
> What's the best way to validate a string contains a IP address 

Don't reinvent the wheel, use the ipaddress module.

py> import ipaddress
py> ipaddress.ip_address('99.99.99.99')
IPv4Address('99.99.99.99')


If the address is not a valid address syntax, it will raise ValueError.


-- 
Steve


More information about the Tutor mailing list