From: Michael Tremer Date: Sun, 22 Jun 2014 22:02:13 +0000 (+0000) Subject: Simplify IP address guessing code. X-Git-Tag: 001~13 X-Git-Url: http://git.ipfire.org/?p=ddns.git;a=commitdiff_plain;h=022003bcb89d4e8f51cc2385d09acd6dbef64e4d Simplify IP address guessing code. --- diff --git a/ddns.in b/ddns.in index 343c396..1ca5f83 100644 --- a/ddns.in +++ b/ddns.in @@ -72,12 +72,12 @@ def main(): # Handle commands... if args.subparsers_name == "guess-ip-addresses": # IPv6 - ipv6_address = d.system.guess_external_ipv6_address() + ipv6_address = d.system.guess_external_ip_address("ipv6") if ipv6_address: print _("IPv6 Address: %s") % ipv6_address # IPv4 - ipv4_address = d.system.guess_external_ipv4_address() + ipv4_address = d.system.guess_external_ip_address("ipv4") if ipv4_address: print _("IPv4 Address: %s") % ipv4_address diff --git a/src/ddns/system.py b/src/ddns/system.py index d11fcf4..b086f5e 100644 --- a/src/ddns/system.py +++ b/src/ddns/system.py @@ -103,19 +103,15 @@ class DDNSSystem(object): return match.group(1) - def guess_external_ipv6_address(self): - """ - Sends a request to the internet to determine - the public IPv6 address. - """ - return self._guess_external_ip_address("http://checkip6.dns.lightningwirelabs.com") + def guess_external_ip_address(self, family, **kwargs): + if family == "ipv6": + url = "http://checkip6.dns.lightningwirelabs.com" + elif family == "ipv4": + url = "http://checkip4.dns.lightningwirelabs.com" + else: + raise ValueError("unknown address family") - def guess_external_ipv4_address(self): - """ - Sends a request to the internet to determine - the public IPv4 address. - """ - return self._guess_external_ip_address("http://checkip4.dns.lightningwirelabs.com") + return self._guess_external_ip_address(url, **kwargs) def send_request(self, url, method="GET", data=None, username=None, password=None, timeout=30): assert method in ("GET", "POST") @@ -256,11 +252,7 @@ class DDNSSystem(object): # If the external IP address should be used, we just do # that. if guess_ip in ("true", "yes", "1"): - if proto == "ipv6": - return self.guess_external_ipv6_address() - - elif proto == "ipv4": - return self.guess_external_ipv4_address() + return self.guess_external_ip_address(proto) # Get the local IP addresses. else: