溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python免登錄怎么實現(xiàn)域名解析

發(fā)布時間:2023-03-09 11:42:34 來源:億速云 閱讀:113 作者:iii 欄目:開發(fā)技術(shù)

本文小編為大家詳細(xì)介紹“Python免登錄怎么實現(xiàn)域名解析”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“Python免登錄怎么實現(xiàn)域名解析”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識吧。

目的是編寫python腳本,通過dnspod api獲取個人域名內(nèi)的dns解析記錄,

免登錄實現(xiàn)域名的解析、修改和刪除:

Python免登錄怎么實現(xiàn)域名解析

為什么要編寫這個腳本?當(dāng)你在公司負(fù)責(zé)很多的域名又經(jīng)常需要解析和查看,頻繁登錄網(wǎng)站去查去修改是一件費神的事。

上圖的賬號內(nèi)有2個域名ssw.fit和ssw.ski,我想給ssw.ski增加了一條A記錄,

把test子域名解析到我的linux云服務(wù)器,添加完后訪問test.ssw.ski

如何獲得域名解析信息

使用dnspod api

#獲取domain_id
curl 'https://dnsapi.cn/Domain.List' -d 'login_token=&format=json'

#獲取record_id
curl 'https://dnsapi.cn/Record.List' -d 'login_token=&format=json&domain_id='

獲取Token

訪問https://console.dnspod.cn/account/token/token,創(chuàng)建一個秘鑰

Python免登錄怎么實現(xiàn)域名解析

完成后程序中可以使用ID,TOKEN來訪問api。

目標(biāo)實現(xiàn)

一般都通過requests 的post方法訪問對應(yīng)網(wǎng)址。

不過這里用curl命令更簡介方便,它也可以發(fā)起post請求,并且一條命令解決。

所以用python來執(zhí)行l(wèi)inux下的curl命令就可以了:

class DomainHandler(object):
    def __init__(self):
        pass

    def exec_cmd(self,cmd):
        res = Popen(cmd, shell=True, stdout=PIPE)
        ret = res.communicate()[0].decode('utf-8')
        return ret.strip()

下面以添加A記錄為例。

添加字典對應(yīng)函數(shù)入口:

dic = {
    '1':DomainHandler().add,
    '2':DomainHandler().mod,
    '3':DomainHandler().delete
}

tag = True
while tag:
    print('''
    1.增加
    2.修改
    3.刪除
    q.退出
    ''')
    choice = input('\033[1;42m輸入選項:\033[0m').strip()
    if not choice:
        continue
    if choice == 'q':
        break
    if choice in dic:
        dic[choice]()

    else:
        print('\033[31m選項不存在\033[0m')

添加記錄的入口函數(shù):

    def add(self):
        self.domain_info()
        while tag:
            self.domain_id = input('\033[1;42m輸入域名ID:\033[0m').strip()
            if self.domain_id == 'q':
                break
            if not self.domain_id or not self.domain_id.isdigit():
                print('\033[31merror id\033[0m')
                continue
            self.sub_domain = input('\033[1;42m子域名[@或*等]:\033[0m').strip()
            self.record_type = input('\033[1;42m類型[A或CNAME]:\033[0m').strip()
            self.address = input('\033[1;42m記錄值(ip或域名):\033[0m').strip()

            if not self.sub_domain or not self.record_type or not self.address:
                print('\033[31m參數(shù)不能為空\033[0m')
                continue
            self.add_Arecord(self.domain_id,self.sub_domain,self.record_type,self.address)
            if self.domain_id == 'q' or self.record_type == 'q' or self.address == 'q':
                self.tag = False
            break

獲取域名信息:

    def domain_info(self):
        cmd = 'curl -s https://dnsapi.cn/Domain.List -d "login_token=391845,92f408bb5343e&format=json"'
        data = json.loads(self.exec_cmd(cmd))
        print(data)
        for item in data['domains']:
            print('%s:%s' % (item['name'], item['id']))

添加記錄:

    def add_Arecord(self,domain_id,sub_domain,record_type,address):
        print(domain_id,sub_domain,record_type,address)
        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(
            domain_id, sub_domain, record_type, address)
        r = json.loads(self.exec_cmd(cmd2))
        print(r['status']['message'])

修改記錄

我想把test.ssw.ski的ip修改為1.1.1.1

Python免登錄怎么實現(xiàn)域名解析

ipconfig/flushdns刷新一下dns緩存,生效了:

Python免登錄怎么實現(xiàn)域名解析

文章還沒寫完,它就檢測到域名未備案了

Python免登錄怎么實現(xiàn)域名解析

讀到這里,這篇“Python免登錄怎么實現(xiàn)域名解析”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領(lǐng)會,如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI