| 主机参考:VPS测评参考推荐/专注分享VPS服务器优惠信息!若您是商家可以在本站进行投稿,查看详情!此外我们还提供软文收录、PayPal代付、广告赞助等服务,查看详情! |
| 我们发布的部分优惠活动文章可能存在时效性,购买时建议在本站搜索商家名称可查看相关文章充分了解该商家!若非中文页面可使用Edge浏览器同步翻译!PayPal代付/收录合作 |
- import json
- import re
- import os
- from sys import exit
- from urllib import request
- from urllib.error import HTTPError
- # Begin settings
- # Get the Production API key/secret from https://developer.godaddy.com/keys/.
- # Ensure it’s for “Production” as first time it’s created for “Test”.
- KEY = None # <API production key>
- SECRET = None # <API secret>
- # set call API header
- HEADERS = {
- “Accept”: “application/json”,
- ‘Content-type’: ‘application/json’,
- ‘Authorization’: ‘sso-key {}:{}’.format(KEY, SECRET)
- }
- # Domain to update.
- DOMAIN = None # <domain name>
- # Advanced settings – change only if you know what you’re doing ?
- # Record type, as seen in the DNS setup page, default A.
- TYPE = ‘A’
- # Record name, as seen in the DNS setup page, default @.
- NAME = ‘@’
- # Time To Live in seconds, minimum default 600 (10mins).
- # If your public IP seldom changes, set it to 3600 (1hr) or more for DNS servers cache performance.
- TTL = 600
- # godaddy API URL
- GOD_ADDY_API_URL = “https://api.godaddy.com/v1/domains/{}/records/{}/{}”.format(DOMAIN, TYPE, NAME)
- # Writable path to last known Public IP record cached. Best to place in tmpfs.
- CACHED_IP_FILE = ‘/tmp/current_ip’
- CACHED_IP = None
- if os.path.isfile(CACHED_IP_FILE):
- CACHED_IP = open(CACHED_IP_FILE, mode=”r”, encoding=”utf-8″).read()
- # External URL to check for current Public IP, must contain only a single plain text IP.
- # Default http://api.ipify.org.
- CHECK_URL = ‘http://api.ipify.org’
- if not KEY or not SECRET:
- print(“Error: Requires API ‘Key/Secret’ value.”)
- exit(1)
- if not DOMAIN:
- print(“Error: Requires ‘Domain’ value.”)
- exit(1)
- # Get Host Public IP
- PUBLIC_IP = request.urlopen(CHECK_URL).read().decode(‘utf-8’)
- regex = r”^d{1,3}.d{1,3}.d{1,3}.d{1,3}$”
- if re.search(regex, PUBLIC_IP):
- print(“Get Public IP: {}”.format(PUBLIC_IP))
- else:
- print(“Fail! Public IP: {}”.format(PUBLIC_IP))
- exit(1)
- # Check if the IP needs to be updated
- if CACHED_IP != PUBLIC_IP:
- req = request.Request(url=GOD_ADDY_API_URL, headers=HEADERS)
- try:
- with request.urlopen(req) as response:
- data = json.loads(response.read().decode(‘utf-8’))
- NAME_BIND_IP = data[0][“data”] if data else None
- except HTTPError as e:
- if e.code in (422, 404):
- NAME_BIND_IP = None
- if NAME_BIND_IP == PUBLIC_IP:
- print(“unchanged! Current ‘Public IP’ matches ‘GoDaddy’ records. No update required!”)
- else:
- print(“changed! Updating ‘{}.{}’, {} to {}”.format(NAME, DOMAIN, NAME_BIND_IP, PUBLIC_IP))
- data = json.dumps([{“data”: PUBLIC_IP, “name”: NAME, “ttl”: TTL, “type”: TYPE}]).encode(‘utf-8’)
- req = request.Request(url=GOD_ADDY_API_URL, data=data, headers=HEADERS, method=’PUT’)
- with request.urlopen(req) as response:
- print(“Success!” if not response.read().decode(‘utf-8’) else “Success!”)
- open(‘/tmp/current_ip’, mode=”w”, encoding=”utf-8″).write(PUBLIC_IP)
- else:
- print(“Current ‘Public IP’ matches ‘Cached IP’ recorded. No update required!”)
复制代码
README
复制代码
贴上GitHub: https://github.com/anshengme/ddns
各路大神的回复:
注册: 好东西,收藏了
ansheng: 一般不是到手cf么 、 不过也谢谢大佬
我是大水逼: 我的域名现在放在狗爹,所以就弄狗爹的了,如果是CF,到时候在撸个CF就好了
这几篇文章你可能也喜欢:
- 暂无相关推荐文章
本文由主机参考刊发,转载请注明:开源个godaddy ddns python脚本 https://dev-web.zhujicankao.com/21140.html
主机参考















评论前必须登录!
注册