Skip to content

Commit

Permalink
fix validipaddr and validipport
Browse files Browse the repository at this point in the history
  • Loading branch information
anandology committed Oct 21, 2008
1 parent dd7a8f3 commit ee95a8e
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions web/net.py
Expand Up @@ -15,21 +15,40 @@
except ImportError: pass

def validipaddr(address):
"""returns True if `address` is a valid IPv4 address"""
"""returns True if `address` is a valid IPv4 address.
>>> validipaddr('192.168.1.1')
True
>>> validipaddr('192.168.1.800')
False
>>> validipaddr('192.168.1')
False
"""
try:
octets = address.split('.')
assert len(octets) == 4
if len(octets) != 4:
return False
for x in octets:
assert 0 <= int(x) <= 255
except (AssertionError, ValueError):
if not (0 <= int(x) <= 255):
return False
except ValueError:
return False
return True

def validipport(port):
"""returns True if `port` is a valid IPv4 port"""
"""returns True if `port` is a valid IPv4 port.
>>> validipport('9000')
True
>>> validipport('foo')
False
>>> validipport('1000000')
False
"""
try:
assert 0 <= int(port) <= 65535
except (AssertionError, ValueError):
if not (0 <= int(port) <= 65535):
return False
except ValueError:
return False
return True

Expand Down

0 comments on commit ee95a8e

Please sign in to comment.