当前仓库属于暂停状态,部分功能使用受限,详情请查阅 仓库状态说明
2 Star 3 Fork 2

月夜行梦/ddns
暂停

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
dnspod.py 3.97 KB
一键复制 编辑 原始数据 按行查看 历史
月夜行梦 提交于 2020-11-24 23:54 . add logging
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import httplib
import urllib
import json, os, logging
import logger
Headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/json"}
def get_domain_id(login_token, domain_name):
conn = httplib.HTTPSConnection("dnsapi.cn")
params = dict(
login_token=login_token,
format="json"
)
conn.request("POST", "/Domain.List", urllib.urlencode(params), Headers)
response = conn.getresponse()
data = json.loads(response.read())
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
conn.close()
def create_domain(login_token, domain_name):
conn = httplib.HTTPSConnection("dnsapi.cn")
params = dict(
login_token=login_token,
format="json",
domain = domain_name
)
conn.request("POST", "/Domain.Create", urllib.urlencode(params), Headers)
response = conn.getresponse()
data = json.loads(response.read())
if int(data['status']['code']) == 1:
return data['domain']['id']
conn.close()
def get_record_value(login_token, domain_id, sub_domain):
conn = httplib.HTTPSConnection("dnsapi.cn")
params = dict(
login_token=login_token,
format="json",
domain_id = domain_id,
sub_domain = sub_domain
)
conn.request("POST", "/Record.List", urllib.urlencode(params), Headers)
response = conn.getresponse()
data = json.loads(response.read())
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"
conn.close()
def get_record_id(login_token, domain_id, sub_domain):
conn = httplib.HTTPSConnection("dnsapi.cn")
params = dict(
login_token=login_token,
format="json",
domain_id = domain_id,
sub_domain = sub_domain
)
conn.request("POST", "/Record.List", urllib.urlencode(params), Headers)
response = conn.getresponse()
data = json.loads(response.read())
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
conn.close()
def create_record_id(login_token, domain_id, sub_domain, localIP):
conn = httplib.HTTPSConnection("dnsapi.cn")
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
)
conn.request("POST", "/Record.Create", urllib.urlencode(params), Headers)
response = conn.getresponse()
data = json.loads(response.read())
if int(data['status']['code']) == 1:
logging.info("Sub_domain [%s] create success" % sub_domain)
else:
logging.error("Sub_domain [%s] create failed" % sub_domain)
conn.close()
def record_ddns(login_token, domain_id, record_id, sub_domain, localIP):
conn = httplib.HTTPSConnection("dnsapi.cn")
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
)
conn.request("POST", "/Record.Ddns", urllib.urlencode(params), Headers)
response = conn.getresponse()
data = json.loads(response.read())
if int(data['status']['code']) == 1:
logging.info("DDns Success for subdomain [%s], IP change to %s" % (sub_domain, localIP))
else:
logging.error("DDns Error for subdomain [%s]: %s" % (sub_domain, data['status']['message']))
conn.close()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/tdg/ddns.git
[email protected]:tdg/ddns.git
tdg
ddns
ddns
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385