From 3e72aa5da5a6e6ed94c2122e732105f90548d9d9 Mon Sep 17 00:00:00 2001 From: Junaid Loonat Date: Tue, 26 Apr 2016 23:12:26 +0200 Subject: [PATCH] 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. --- src/utils/net.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/utils/net.py b/src/utils/net.py index 2ec083072..c40a29e6b 100644 --- a/src/utils/net.py +++ b/src/utils/net.py @@ -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.