Skip to content

Commit a89059c

Browse files
author
Andreea Dima
authored
Do not query DNS if last_succes in the last minute (#9)
Do not query DNS if last_succes in the last minute
1 parent a82b6c0 commit a89059c

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

dyndns/domain_update.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,21 @@ def main(domain, settings='settings.txt'):
2727
return "Domain {} configured incorectly. Rerun setup.".format(domain)
2828
print("Read {} config.".format(domain))
2929

30-
# read existing ip for domain
30+
# read existing ip for domain from config || from DNS if last check was less than 60 sec ago
3131
ip = None
32-
try:
33-
answers = dns.resolver.query(domain, 'A')
34-
if not answers:
35-
return "No A record found for domain {}".format(domain)
36-
ip = answers[0]
37-
print("IP {} found in A record".format(ip))
38-
except Exception:
39-
print("No A record found for domain {}".format(domain))
32+
if 'last_success' in config[domain] and int(time.time()) - config[domain]['last_success'] < 60:
33+
ip = config[domain]['ip']
34+
print("Recently used IP {}".format(ip))
35+
else:
36+
try:
37+
answers = dns.resolver.query(domain, 'A')
38+
if not answers:
39+
return "No A record found for domain {}".format(domain)
40+
ip = answers[0]
41+
print("IP {} found in A record".format(ip))
42+
config[domain]['last_dns_check'] = int(time.time())
43+
except Exception:
44+
print("No A record found for domain {}".format(domain))
4045

4146
# get public ip
4247
response = requests.get("https://api.ipify.org", params={'format': 'json'})
@@ -46,8 +51,6 @@ def main(domain, settings='settings.txt'):
4651
if not public_ip:
4752
return "Could not discover public IP."
4853

49-
config[domain]['last_dns_check'] = int(time.time())
50-
5154
print("New IP: {}".format(public_ip))
5255
if ip and str(ip) == str(public_ip):
5356
config[domain]['ip'] = public_ip

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
setup(
88
name = 'domain-connect-dyndns',
9-
version = '0.0.2',
9+
version = '0.0.3',
1010
description = 'Python client library for Dynamic DNS using Domain Connect',
1111
license = 'MIT',
1212
long_description=long_description,

0 commit comments

Comments
 (0)