Refactor Port number validation

Leverage the recently added _is_int_in_range() function to simpify valid range check

Change-Id: I8b7d81a5dc38598275491291c672d9d885931b10
This commit is contained in:
Ronald Bradford 2015-11-13 13:49:08 -05:00
parent c5d9e6cde8
commit 49bdee0160
1 changed files with 11 additions and 13 deletions

View File

@ -177,19 +177,6 @@ def is_valid_ip(address):
return is_valid_ipv4(address) or is_valid_ipv6(address)
def is_valid_port(port):
"""Verify that port represents a valid port number.
.. versionadded:: 1.1.1
"""
try:
val = int(port)
except (ValueError, TypeError):
return False
return (val > 0 and val <= 65535)
def _is_int_in_range(value, start, end):
"""Try to convert value to int and check if it lies within
range 'start' to 'end'.
@ -206,6 +193,17 @@ def _is_int_in_range(value, start, end):
return (start <= val <= end)
def is_valid_port(port):
"""Verify that port represents a valid port number.
Port can be valid integer having a value of 1 up to and
including 65535.
.. versionadded:: 1.1.1
"""
return _is_int_in_range(port, 1, 65535)
def is_valid_icmp_type(type):
"""Verify if ICMP type is valid.