Table of Contents
如何获得域名解析信息
获取Token
目标实现
修改记录
Home Backend Development Python Tutorial How to implement domain name resolution without login in Python

How to implement domain name resolution without login in Python

May 30, 2023 pm 09:07 PM
python

目的是编写python脚本,通过dnspod api获取个人域名内的dns解析记录,

免登录实现域名的解析、修改和删除:

How to implement domain name resolution without login in Python

为什么要编写这个脚本?当你在公司负责很多的域名又经常需要解析和查看,频繁登录网站去查去修改是一件费神的事。

上图的账号内有2个域名ssw.fit和ssw.ski,我想給ssw.ski增加了一条A记录,

把test子域名解析到我的linux云服务器,添加完后访问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='
Copy after login

获取Token

访问https://console.dnspod.cn/account/token/token,创建一个秘钥

How to implement domain name resolution without login in Python

完成后程序中可以使用ID,TOKEN来访问api。

目标实现

一般都通过requests 的post方法访问对应网址。

不过这里用curl命令更简介方便,它也可以发起post请求,并且一条命令解决。

所以用python来执行linux下的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()
Copy after login

下面以添加A记录为例。

添加字典对应函数入口:

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')
Copy after login

添加记录的入口函数:

    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参数不能为空\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
Copy after login

获取域名信息:

    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']))
Copy after login

添加记录:

    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'])
Copy after login

修改记录

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

How to implement domain name resolution without login in Python

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

How to implement domain name resolution without login in Python

文章还没写完,它就检测到域名未备案了

How to implement domain name resolution without login in Python

The above is the detailed content of How to implement domain name resolution without login in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1252
24
PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

How to run sublime code python How to run sublime code python Apr 16, 2025 am 08:48 AM

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Golang vs. Python: Performance and Scalability Golang vs. Python: Performance and Scalability Apr 19, 2025 am 12:18 AM

Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

How to run python with notepad How to run python with notepad Apr 16, 2025 pm 07:33 PM

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".

See all articles