Skip to content

Commit

Permalink
Update is_ip to support interface name as well
Browse files Browse the repository at this point in the history
  • Loading branch information
doadin committed Feb 13, 2020
1 parent 2f1c008 commit 633e312
Showing 1 changed file with 38 additions and 4 deletions.
42 changes: 38 additions & 4 deletions deluge/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -905,10 +905,10 @@ def free_space(path):


def is_ip(ip):
"""A test to see if 'ip' is a valid IPv4 or IPv6 address.
"""A test to see if 'ip' is a valid IPv4 or IPv6 address or network interface.
Args:
ip (str): The IP to test.
ip (str): The IP or interface to test.
Returns:
bool: Whether IP is valid is not.
Expand All @@ -918,10 +918,12 @@ def is_ip(ip):
True
>>> is_ip("2001:db8::")
True
>>> is_ip("eth0")
True
"""

return is_ipv4(ip) or is_ipv6(ip)
return is_ipv4(ip) or is_ipv6(ip) or is_net_interface(ip)


def is_ipv4(ip):
Expand Down Expand Up @@ -984,6 +986,38 @@ def is_ipv6(ip):

return False

def is_net_interface(ip):
"""A test to see if 'ip' is a valid network interface.
Args:
ip (str): The Interface to test. eg. eth0 linux. GUID on Windows.
Returns:
bool: Whether Interface is valid or not.
Examples:
>>> is_net_interface("eth0")
True
>>> is_net_interface("7A30AE62-23ZA-3744-Z844-A5B042524871")
True
"""

import netifaces
try:
import netifaces
except ImportError:
log.error('Unable to verify interfaces. Module netifaces missing')
return True
if windows_check() and not ip.isupper():
log.error('Interface is not uppercase you might be looking for: %s', ip.upper())
return False
if ip in netifaces.interfaces():
return True
else:
log.error('Interface not found!')
return False


def decode_bytes(byte_str, encoding='utf8'):
"""Decodes a byte string and return unicode.
Expand Down Expand Up @@ -1372,4 +1406,4 @@ def is_process_running(pid):
except OSError:
return False
else:
return True
return True

0 comments on commit 633e312

Please sign in to comment.