Skip to content

Commit d415975

Browse files
committedMar 8, 2023
提交代码
1 parent d16c11a commit d415975

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed
 

‎fans/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Python技术 公众号文章代码库
1010

1111
## 实例代码
1212

13+
[用python免登录实现域名解析](https://github.com/JustDoPython/python-examples/tree/master/fans/dns):用python免登录实现域名解析
1314

1415

1516
[美女同事又找我帮忙了,激动!](https://github.com/JustDoPython/python-examples/tree/master/fans/filenaming):美女同事又找我帮忙了,激动!

‎fans/dns/demo.py

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# -*- coding:utf-8 -*-
2+
3+
import optparse,os,json
4+
from subprocess import *
5+
6+
class DomainHandler(object):
7+
def __init__(self):
8+
pass
9+
10+
def exec_cmd(self,cmd):
11+
res = Popen(cmd, shell=True, stdout=PIPE)
12+
ret = res.communicate()[0].decode('utf-8')
13+
return ret.strip()
14+
15+
def domain_info(self):
16+
cmd = 'curl -s https://dnsapi.cn/Domain.List -d "login_token=391845,92f408bb5343e&format=json"'
17+
data = json.loads(self.exec_cmd(cmd))
18+
print(data)
19+
for item in data['domains']:
20+
print('%s:%s' % (item['name'], item['id']))
21+
22+
def add_Arecord(self,domain_id,sub_domain,record_type,address):
23+
print(domain_id,sub_domain,record_type,address)
24+
cmd2 = "curl -s -X POST https://dnsapi.cn/Record.Create -d 'login_token=391845,92f408bb5343e&format=json&domain_id={0}&sub_domain={1}&record_type={2}&record_line_id=0&value={3}'".format(
25+
domain_id, sub_domain, record_type, address)
26+
r = json.loads(self.exec_cmd(cmd2))
27+
print(r['status']['message'])
28+
29+
def add(self):
30+
self.domain_info()
31+
while tag:
32+
self.domain_id = input('\033[1;42m输入域名ID:\033[0m').strip()
33+
if self.domain_id == 'q':
34+
break
35+
if not self.domain_id or not self.domain_id.isdigit():
36+
print('\033[31merror id\033[0m')
37+
continue
38+
self.sub_domain = input('\033[1;42m子域名[@或*等]:\033[0m').strip()
39+
self.record_type = input('\033[1;42m类型[A或CNAME]:\033[0m').strip()
40+
self.address = input('\033[1;42m记录值(ip或域名):\033[0m').strip()
41+
42+
if not self.sub_domain or not self.record_type or not self.address:
43+
print('\033[31m参数不能为空\033[0m')
44+
continue
45+
self.add_Arecord(self.domain_id,self.sub_domain,self.record_type,self.address)
46+
if self.domain_id == 'q' or self.record_type == 'q' or self.address == 'q':
47+
self.tag = False
48+
break
49+
50+
def get_records(self):
51+
self.domain_info()
52+
flag = True
53+
while tag:
54+
if not flag:
55+
break
56+
self.domain_id = input('\033[1;42m输入域名ID:\033[0m').strip()
57+
if self.domain_id == 'q':
58+
break
59+
if not self.domain_id or not self.domain_id.isdigit():
60+
print('\033[31merror id\033[0m')
61+
continue
62+
self.sub_domain = input('\033[1;42m子域名[@或*等]:\033[0m').strip()
63+
self.record_type = input('\033[1;42m类型[A或CNAME]:\033[0m').strip()
64+
cmd3 = "curl -s -X POST https://dnsapi.cn/Record.List -d 'login_token=391845,92f408bb5343e&format=json&domain_id={0}&sub_domain={1}&record_type={2}&offset=0&length=3'".format(
65+
self.domain_id, self.sub_domain, self.record_type)
66+
records = json.loads(self.exec_cmd(cmd3))
67+
try:
68+
print('\033[33m共%s条%s记录\033[0m' % (len(records['records']), self.record_type))
69+
except Exception as e:
70+
print('\033[31m查无此记录\033[0m')
71+
continue
72+
for record in records['records']:
73+
print('\033[35mID{0}: {1}{split}{2}{split}{3}\033[0m'.format(record['id'], record['name'], record['type'],record['value'], split=' ' * 10))
74+
return records
75+
76+
def mod(self):
77+
records = self.get_records()
78+
while tag:
79+
record_id = input('\033[1;42m输入record ID:\033[0m').strip()
80+
if record_id == 'q':
81+
break
82+
value = input("\033[1;42m输入新的record value:\033[0m").strip()
83+
if value == 'q':
84+
break
85+
cmd4 = "curl -s -X POST https://dnsapi.cn/Record.Modify -d 'login_token=391845,92f408bb5343e&format=json&domain_id={0}&record_id={1}&sub_domain={2}&value={3}&record_type={4}&record_line_id=0'".format(self.domain_id,record_id,self.sub_domain,value,self.record_type)
86+
r = json.loads(self.exec_cmd(cmd4))
87+
print(r['status']['message'])
88+
flag = False
89+
break
90+
def delete(self):
91+
records = self.get_records()
92+
while tag:
93+
record_id = input('\033[1;42m输入record ID:\033[0m').strip()
94+
if record_id == 'q':
95+
break
96+
cmd5 = "curl -s -X POST https://dnsapi.cn/Record.Remove -d 'login_token=391845,92f408bb5343e&format=json&domain_id={0}&record_id={1}'".format(self.domain_id,record_id)
97+
r = json.loads(self.exec_cmd(cmd5))
98+
print(r['status']['message'])
99+
flag = False
100+
break
101+
102+
dic = {
103+
'1':DomainHandler().add,
104+
'2':DomainHandler().mod,
105+
'3':DomainHandler().delete
106+
}
107+
108+
tag = True
109+
while tag:
110+
print('''
111+
1.增加
112+
2.修改
113+
3.删除
114+
q.退出
115+
''')
116+
choice = input('\033[1;42m输入选项:\033[0m').strip()
117+
if not choice:
118+
continue
119+
if choice == 'q':
120+
break
121+
if choice in dic:
122+
dic[choice]()
123+
124+
else:
125+
print('\033[31m选项不存在\033[0m')

0 commit comments

Comments
 (0)
Please sign in to comment.