diff options
Diffstat (limited to 'deluge/ui/gtk3/common.py')
-rw-r--r-- | deluge/ui/gtk3/common.py | 29 |
1 files changed, 28 insertions, 1 deletions
diff --git a/deluge/ui/gtk3/common.py b/deluge/ui/gtk3/common.py index 855f6680e..0be3bff35 100644 --- a/deluge/ui/gtk3/common.py +++ b/deluge/ui/gtk3/common.py @@ -29,7 +29,7 @@ from gi.repository.Gtk import ( SortType, ) -from deluge.common import PY2, get_pixmap, osx_check, windows_check +from deluge.common import PY2, get_pixmap, is_ip, osx_check, windows_check log = logging.getLogger(__name__) @@ -393,3 +393,30 @@ def get_clipboard_text(): def windowing(like): return like.lower() in str(type(Display.get_default())).lower() + + +def parse_ip_port(text): + """Return an IP and port from text. + + Parses both IPv4 and IPv6. + + Params: + text (str): Text to be parsed for IP and port. + + Returns: + tuple: (ip (str), port (int)) + + """ + if '.' in text: + # ipv4 + ip, __, port = text.rpartition(':') + elif '[' in text: + # ipv6 + ip, __, port = text.partition('[')[2].partition(']:') + else: + return None, None + + if ip and is_ip(ip) and port.isdigit(): + return ip, int(port) + else: + return None, None |