Created
December 27, 2013 14:37
-
-
Save n9986/8147782 to your computer and use it in GitHub Desktop.
Validate IP:PORT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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