0

我正在使用新的getdns Python API。

我特别想得到DNSSEC_BOGUS答案。为此,我需要查看results.reply_tree.reply[n]['answer']['rdata']['ipv4_address']元素。这是 IPv4 地址的 4 字节二进制表示。getdns 中是否有一种简单的方法可以将其更改为可打印的 IPv4 地址?

我不想使用该results.just_address_answers字段,因为不能保证地址的顺序相同。

这是我的示例代码:

import getdns, sys
def get_ip(hostname, ctx, extensions):
    print("host: {} extensions: {}".format(hostname, extensions))
    results = ctx.address(name=hostname, extensions=extensions)
    print("addresses:")
    for addr in results.just_address_answers:
        print("  {}".format(addr['address_data']))
    print("replies:")
    for reply in results.replies_tree:
        for a in reply['answer']:
            if a['type']==getdns.RRTYPE_A:
                try:
                    print("  type={} data={} dnssec_status={}".format(a['type'], a['rdata']['ipv4_address'], reply['dnssec_status']))
                except KeyError:
                    print("  no dnssec_status")
    print("")

if __name__=="__main__":
    print("getdns.DNSSEC_SECURE={}".format(getdns.DNSSEC_SECURE))
    print("getdns.DNSSEC_INDETERMINATE={}".format(getdns.DNSSEC_INDETERMINATE))
    print("getdns.DNSSEC_INSECURE={}".format(getdns.DNSSEC_INSECURE))
    print("getdns.DNSSEC_BOGUS={}".format(getdns.DNSSEC_BOGUS))
    failed = 'www.dnssec-failed.org'
    ctx = getdns.Context()
    get_ip(failed, ctx, {})
    get_ip(failed, ctx, {'dnssec_return_status' : getdns.EXTENSION_TRUE })
    get_ip(failed, ctx, {"dnssec_return_validation_chain" : getdns.EXTENSION_TRUE})

虽然这可行,但我真的希望data=打印出来的是 IPv4 地址,而不仅仅是四个二进制字节。

4

1 回答 1

1
bin_addr = results.reply_tree.reply[n]['answer']['rdata']['ipv4_address']
string_addr = '.'.join(map(str, map(ord, bin_addr)))
于 2015-05-13T20:46:57.170 回答