您如何使用GetDnSapi Python API使IPv4地址可打印



我正在使用新的getdns python api。

我专门试图获得DNSSEC_BOGUS答案。为此,我需要查看results.reply_tree.reply[n]['answer']['rdata']['ipv4_address']元素。这是IPv4地址的4字节二进制表示。GetDN中是否有一种简单的方法将其更改为可打印的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})

尽管这起作用,但我真的想要打印以打印IPv4地址的data=,而不仅仅是四个二进制字节。

bin_addr = results.reply_tree.reply[n]['answer']['rdata']['ipv4_address']
string_addr = '.'.join(map(str, map(ord, bin_addr)))

相关内容

  • 没有找到相关文章

最新更新