1 Star 1 Fork 0

月夜行梦/ddns-py3

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
dnspod.py 3.99 KB
一键复制 编辑 原始数据 按行查看 历史
月夜行梦 提交于 2020-11-25 00:12 +08:00 . add logger
#!/usr/bin/python
# -*- coding: UTF-8 -*-
from urllib import request, parse
import json, os, logging
import logger
Headers = {
'Accept': 'text/json',
'Content-type': 'application/x-www-form-urlencoded',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'
}
def getRequest(api, data):
return request.Request(url=f'https://dnsapi.cn/{api}', data=parse.urlencode(data).encode('utf-8'), headers=Headers, method='POST')
def get_domain_id(login_token, domain_name):
params = dict(
login_token=login_token,
format="json"
)
req = getRequest('Domain.List', params)
response = request.urlopen(req)
data = json.loads(response.read().decode('utf-8'))
if int(data['status']['code']) == 1:
domails = data['domains']
for domain in domails:
if domain['name'] == domain_name:
return domain['id']
return 0
else:
return 0
def create_domain(login_token, domain_name):
params = dict(
login_token=login_token,
format="json",
domain = domain_name
)
req = getRequest('Domain.Create', params)
response = request.urlopen(req)
data = json.loads(response.read().decode('utf-8'))
if int(data['status']['code']) == 1:
return data['domain']['id']
else:
return 0
def get_record_value(login_token, domain_id, sub_domain):
params = dict(
login_token=login_token,
format="json",
domain_id = domain_id,
sub_domain = sub_domain
)
req = getRequest('Record.List', params)
response = request.urlopen(req)
data = json.loads(response.read().decode('utf-8'))
if int(data['status']['code']) == 1:
records = data['records']
for record in records:
if record['type'] == 'A' and record['name'] == sub_domain:
return record['value']
return "127.0.0.1"
else:
return "127.0.0.1"
def get_record_id(login_token, domain_id, sub_domain):
params = dict(
login_token=login_token,
format="json",
domain_id = domain_id,
sub_domain = sub_domain
)
req = getRequest('Record.List', params)
response = request.urlopen(req)
data = json.loads(response.read().decode('utf-8'))
if int(data['status']['code']) == 1:
records = data['records']
for record in records:
if record['type'] == 'A' and record['name'] == sub_domain:
return record['id']
return 0
else:
return 0
def create_record_id(login_token, domain_id, sub_domain, localIP):
params = dict(
login_token=login_token,
format="json",
domain_id = domain_id,
sub_domain = sub_domain,
record_type = 'A',
record_line_id = "0",
value = localIP
)
req = getRequest('Record.Create', params)
response = request.urlopen(req)
data = json.loads(response.read().decode('utf-8'))
if int(data['status']['code']) == 1:
logging.info(f"Sub_domain [{sub_domain}] create success")
else:
logging.error(f"Sub_domain [{sub_domain}] create failed")
def record_ddns(login_token, domain_id, record_id, sub_domain, localIP):
req = getRequest('Record.Ddns')
params = dict(
login_token=login_token,
format="json",
domain_id = domain_id,
record_id = record_id,
sub_domain = sub_domain,
record_line_id = "0",
value = localIP
)
req = getRequest('Record.Ddns', params)
response = request.urlopen(req)
data = json.loads(response.read().decode('utf-8'))
if int(data['status']['code']) == 1:
logging.info(f"DDns Success for subdomain [{sub_domain}], IP change to {localIP}")
else:
logging.error(f"DDns Error for subdomain [{sub_domain}]: {data['status']['message']}")
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/tdg/ddns-py3.git
git@gitee.com:tdg/ddns-py3.git
tdg
ddns-py3
ddns-py3
master

搜索帮助