> For the complete documentation index, see [llms.txt](https://close.gitbook.io/yun-wei-bi-ji/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://close.gitbook.io/yun-wei-bi-ji/python/python-xiao-ji-qiao/dnspython-shi-xian-ns-ji-lu-cha-xun.md).

# dnspython实现NS记录查询

```python
pip install dnspython
```

> query(self, qname, rdtype = 1, rdclass = 1, tcp = False, source = None,raise\_on\_no\_answer = True, source\_port = 0)
>
> qname参数为查询的域名，
>
> rdtype参数用来指定RR资源。
>
> &#x20;A记录：将主机名转换成IP地址&#x20;
>
> MX记录：邮件交换记录，定义邮件服务器的域名&#x20;
>
> CNAME记录：别名记录，实现域名间的映射&#x20;
>
> NS记录：标记区域的域名服务器及授权子域
>
> PTR记录：反向解析，与A记录相反，将IP地址转换为主机名 SOA记录：
>
> SOA标记，一个起始授权区的定义
>
> rdclass指定网络类型，IN，CH，HS， IN默认 tcp指定查询是否启用TCP协议（默认不启用） source 与source\_port指定查询源的地址与端口，默认为查询设备的IP地址和0 raise\_on\_no\_answer指定查询无应答时是否触发异常，默认为True

### 查询NS记录

```python
import dns.resolver

domain = raw_input('Please input an domain: ')
ns = dns.resolver.query(domain, 'NS')
for i in ns.response.answer:
    for j in i.items:
        print(j.to_text())
```

**测试**：

```python
[root@bogon dns]# python NS.py
Please input an domain: baidu.com #智能输入一级域名
ns4.baidu.com.
ns3.baidu.com.
ns2.baidu.com.
dns.baidu.com.
ns7.baidu.com.
```

### 查询A记录

```python
import dns.resolver

domain = raw_input(‘Please input an domain:’)

A = dns.resolver.query(domain, ‘A’)
for i in A.response.answer:
    for j in i.items:
        print(j.address)
```

### 查询 CNAME 记录

* 只限输入一级域名

```python
import dns.resolver

domain = raw_input('please input a domain:')

CNAME = dns.resolver.query(domain,'CNAME')
for i in CNAME.response.answer:
    for j in i.items:
        print(j.to_text())
```

### MX记录

```python
import dns.resolver

domain = raw_input('Please input a domain')

MX = dns.resolver.query(domain, 'MX')

for i in MX:
    print('MX preference =', i.preference, 'mail exchanger =', i.exchange
)
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://close.gitbook.io/yun-wei-bi-ji/python/python-xiao-ji-qiao/dnspython-shi-xian-ns-ji-lu-cha-xun.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
