socket.gethostbyaddr #18026
Replies: 4 comments 3 replies
-
Hi |
Beta Was this translation helpful? Give feedback.
-
Ah ok!
Hope that help you |
Beta Was this translation helpful? Give feedback.
-
Are you a bot? |
Beta Was this translation helpful? Give feedback.
-
Micropython only has socket.getaddrinfo; it does not have any others. You need to implement socket.gethostbyaddr using a low-level UDP DNS query on port 53 to a DNS server. Alternatively, you can use the web API. # dns_web_api.py
import time
import wifi # connect to your wifi
time.sleep(5)
import requests
def reverse_ip_to_ptr(ip):
return '.'.join(ip.split('.')[::-1]) + '.in-addr.arpa'
# Forward DNS lookup example
domain = 'one.one.one.one'
print(domain)
forward_url = f'https://dns.google/resolve?name={domain}&type=A'
resp = requests.get(forward_url)
forward_data = resp.json()
print(forward_data['Answer'])
# Reverse DNS lookup example
ip = '1.1.1.1'
print(ip)
rev_ip = reverse_ip_to_ptr(ip)
reverse_url = f'https://dns.google/resolve?name={rev_ip}&type=PTR'
resp = requests.get(reverse_url)
reverse_data = resp.json()
print(reverse_data['Answer']) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi, how should I replace the socket.gethostbyaddr function from full python? (I'm running a hotspot and a wifi client networks)
Beta Was this translation helpful? Give feedback.
All reactions