Skip to content

Instantly share code, notes, and snippets.

@n9986
Created December 27, 2013 14:37
Show Gist options
  • Save n9986/8147782 to your computer and use it in GitHub Desktop.
Save n9986/8147782 to your computer and use it in GitHub Desktop.
Validate IP:PORT
import re
b = '123.0.1.00:1212'
naiveip_re = re.compile(r"""^(?:
(?P<addr>
(?P<ipv4>\d{1,3}(?:\.\d{1,3}){3}) | # IPv4 address
(?P<ipv6>\[[a-fA-F0-9:]+\]) | # IPv6 address
(?P<fqdn>[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*) # FQDN
):)?(?P<port>\d+)$""", re.X)
m = re.match(naiveip_re, b)
if m is None:
print '"%s" is not a valid port number or address:port pair.' % b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment