Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd415975

Browse files
committed
提交代码
1 parentd16c11a commitd415975

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

‎fans/README.md‎

Lines changed: 1 addition & 0 deletions
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‎

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# -*- coding:utf-8 -*-
2+
3+
importoptparse,os,json
4+
fromsubprocessimport*
5+
6+
classDomainHandler(object):
7+
def__init__(self):
8+
pass
9+
10+
defexec_cmd(self,cmd):
11+
res=Popen(cmd,shell=True,stdout=PIPE)
12+
ret=res.communicate()[0].decode('utf-8')
13+
returnret.strip()
14+
15+
defdomain_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+
foritemindata['domains']:
20+
print('%s:%s'% (item['name'],item['id']))
21+
22+
defadd_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+
defadd(self):
30+
self.domain_info()
31+
whiletag:
32+
self.domain_id=input('\033[1;42m输入域名ID:\033[0m').strip()
33+
ifself.domain_id=='q':
34+
break
35+
ifnotself.domain_idornotself.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+
ifnotself.sub_domainornotself.record_typeornotself.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+
ifself.domain_id=='q'orself.record_type=='q'orself.address=='q':
47+
self.tag=False
48+
break
49+
50+
defget_records(self):
51+
self.domain_info()
52+
flag=True
53+
whiletag:
54+
ifnotflag:
55+
break
56+
self.domain_id=input('\033[1;42m输入域名ID:\033[0m').strip()
57+
ifself.domain_id=='q':
58+
break
59+
ifnotself.domain_idornotself.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+
exceptExceptionase:
70+
print('\033[31m查无此记录\033[0m')
71+
continue
72+
forrecordinrecords['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+
returnrecords
75+
76+
defmod(self):
77+
records=self.get_records()
78+
whiletag:
79+
record_id=input('\033[1;42m输入record ID:\033[0m').strip()
80+
ifrecord_id=='q':
81+
break
82+
value=input("\033[1;42m输入新的record value:\033[0m").strip()
83+
ifvalue=='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+
defdelete(self):
91+
records=self.get_records()
92+
whiletag:
93+
record_id=input('\033[1;42m输入record ID:\033[0m').strip()
94+
ifrecord_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+
whiletag:
110+
print('''
111+
1.增加
112+
2.修改
113+
3.删除
114+
q.退出
115+
''')
116+
choice=input('\033[1;42m输入选项:\033[0m').strip()
117+
ifnotchoice:
118+
continue
119+
ifchoice=='q':
120+
break
121+
ifchoiceindic:
122+
dic[choice]()
123+
124+
else:
125+
print('\033[31m选项不存在\033[0m')

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp