Use dns.name.from_text mutiple time

44 views
Skip to first unread message

Hugo Dos Santos

unread,
Jul 28, 2023, 11:59:25 AM7/28/23
to dnspython-users
Hello, 

I try to retrieve all the DNS fields of a domain name using the DNS package on python.

I found on the doc the code for retrive one field : 

import dns.resolver

n = dns.name.from_text('lunivhairdegwen.fr')

dns = dns.resolver.resolve(n,'ns')

for rdata in dns:
print(rdata.to_text())
break

How can i take all the fields ? 

if i tried to add an other loop : 

import dns.resolver

n = dns.name.from_text('lunivhairdegwen.fr')

dns = dns.resolver.resolve(n,'ns')

for rdata in dns:
print(rdata.to_text())
break


n = dns.name.from_text('google.fr')

a = dns.resolver.resolve(n,'A')

for rdata in dns:
print(rdata.to_text())
break
 
I have this error : 

    n = dns.name.from_text('google.fr')
        ^^^^^^^^^^^^^^^^^^
AttributeError: 'Name' object has no attribute 'from_text'. Did you mean: 'to_text'?

Any ideas ? 

Thx ! 


Anand Buddhdev

unread,
Jul 28, 2023, 1:09:19 PM7/28/23
to Hugo Dos Santos, dnspython-users
On 28/07/2023 17:59, Hugo Dos Santos wrote:

Hi Hugo,

> I try to retrieve all the DNS fields of a domain name using the DNS package
> on python.
>
> I found on the doc the code for retrive one field :
>
> import dns.resolver

Here, you're importing the "dns" module. So far so good.

> n = dns.name.from_text('lunivhairdegwen.fr')

Here, you're using the "from_text" function of the dns.name module.

> dns = dns.resolver.resolve(n,'ns')

Boom! Here you're overwriting the "dns" module namespace with a new
variable. So in effect, you're unloading the entire "dns" module.

[snip]

> I have this error :
>
> n = dns.name.from_text('google.fr')
> ^^^^^^^^^^^^^^^^^^
> AttributeError: 'Name' object has no attribute 'from_text'. Did you mean:
> 'to_text'?

This is because you overwrote the dnspython "dns" module with a new
variable altogether. You need to use other variable names.

I don't know whether to call this a feature or bug of python, but it's
not the first time someone has made this mistake.

Regards,
Anand

Hugo Dos Santos

unread,
Jul 28, 2023, 1:17:27 PM7/28/23
to dnspython-users
Thx for the answer ! 

What do you mean by " You need to use other variable names.". 

If I try with an other variable names, i have the same error : 

import dns.resolver


n = dns.name.from_text('lunivhairdegwen.fr')

dns = dns.resolver.query(n,'ns')


for rdata in dns:
print(rdata.to_text())


d = dns.name.from_text('lunivhairdegwen.fr')

a = dns.resolver.query(d,'A')

for rdata in a:
print(rdata.to_text())

Anand Buddhdev

unread,
Jul 28, 2023, 1:53:27 PM7/28/23
to Hugo Dos Santos, dnspython-users
On 28/07/2023 19:17, Hugo Dos Santos wrote:

> Thx for the answer !
>
> What do you mean by " You need to use other variable names.".
>
> If I try with an other variable names, i have the same error :
>
> import dns.resolver
>
> n = dns.name.from_text('lunivhairdegwen.fr')
>
> dns = dns.resolver.query(n,'ns')

Here! See this above ^^^

Don't do:

dns = dns.resolver.query(n,'ns')

The result of the "dns.resolver.query" function will OVERWRITE the
entire "dns" module. After that, you can no longer use the "dns" module!
Instead, do something like:

response = dns.resolver.query(n,'ns')

This means, use variable names that are NOT called "dns".

Regards,
Anand

Hugo Dos Santos

unread,
Jul 28, 2023, 5:07:04 PM7/28/23
to dnspython-users
Ahhhhh yes it's logic :'( 

It's working now thx ! 

Reply all
Reply to author
Forward
0 new messages