New isSocketAddress() method

Introduction of the isSocketAddress() method which will use the existing getSocket() method in order to validate whether the provided string is a valid socket address.
This commit is contained in:
Junaid Loonat 2016-04-26 23:12:26 +02:00
parent 7be7b98e68
commit 3e72aa5da5
1 changed files with 11 additions and 0 deletions

View File

@ -77,6 +77,17 @@ def getSocket(host, port=None, socks_proxy=None, vhost=None, vhostv6=None):
else:
raise socket.error('Something wonky happened.')
def isSocketAddress(s):
if ':' in s:
host, port = s.rsplit(':', 1)
try:
int(port)
sock = getSocket(host, port)
return True
except (ValueError, socket.error):
pass
return False
def isIP(s):
"""Returns whether or not a given string is an IP address.