Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dnspython实例代码中查询A记录无效 #1

Open
everXu opened this issue Jun 5, 2015 · 4 comments
Open

dnspython实例代码中查询A记录无效 #1

everXu opened this issue Jun 5, 2015 · 4 comments

Comments

@everXu
Copy link

everXu commented Jun 5, 2015

在代码中使用:
A = dns.resolver.query(domain, 'A')
查询A记录,但实际上查询条件:'A' 无效,得到的结果集中包含CNAME记录,导致出现:

Please input an domain: www.baidu.com
Traceback (most recent call last):
File "aquery.py", line 9, in
print j.address
AttributeError: 'CNAME' object has no attribute 'address'
root@i-7n13qavy:/opt/soft/dnspython-1.9.4# vim aquery.py

异常。
执行环境:Python 2.7.3 , linux Ubuntu 12.04

期待得带你的回复

@wddphero
Copy link

py

@yorkoliu
Copy link
Owner

yorkoliu commented Aug 7, 2016

确认域名是A记录解析的,www.baidu.com是CNAME,所以会报错。

@mapleflow
Copy link

mapleflow commented Nov 5, 2018

def get_ip_list(domain=''):
    print('process domain=' + domain)
    iplist = []
    try:
        A = dns.resolver.query(domain, 'A')
    except Exception as msg:
        print('dns error: ' + msg)
        return []
    for i in A.response.answer:
        for j in i.items:
            # ip
            if 1 == j.rdtype:
                iplist.append(j.address)
            # cname,递归处理
            elif 5 == j.rdtype:
                iplist.extend(get_ip_list(j.to_text()))                  
    return iplist

@yorkoliu
Copy link
Owner

顶mapleflow,兼容性强很多了。 @mapleflow

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants