Learn How to Manage DDNS Using Python

DDNS stands for Dynamic Domain Name System or Dynamic Domain Name Server. Here we are going to learn about DDNS using Python. It is useful to get the IP address of any website. So that we are going to these in python.

DDNS is useful to translate domain names to IP addresses. Basically when we type a domain name into the browser. It searches on the thousands of servers available and gives us an exact IP address. For example, when we type a website name as www.google.com it translates and gives us an exact IP address.

What is DDNS?

DDNS means Dynamic Domain Name Server that is useful to get the IP address of the website. It always points a domain to your current dynamic IP address. When we type some web address in our web browser, the DNS server will search through its database to finding a matching IP address for that domain name.

What is an IP address?

An IP address is nothing but a physical location on the internet. For example, if someone wants to send us a letter then he/she should have our home address. Similarly, our computer also needs an address so that computers on the internet can send us a file that we want to download is called an IP address.

Basic usage of Python ddns module

Python has a DNS toolkit that is dnspython. This module supports all record types and can also be used for dynamic updates, queries, and zone transfers. This module provides high-level and low-level access to DNS. The low-level classes allow messages, direct manipulation of DNS zones, records, and names whereas high-level classes perform queries for data for a given class, name and type, and return an answer set.

Example for basic DNS queries

import dns.resolver
hostname = 'google.org'
for qtype in 'A', 'AAAA', 'MX', 'NS', 'TXT', 'SOA':
    query = dns.resolver.resolve(hostname,qtype, raise_on_no_answer=False)
    if query.rrset is not None:
        print(query.rrset)

The above-given code is about basic DNS queries. Here we are giving a hostname as google.org and it will display the A, AAA, MX, NS, TXT, SOA for the given hostname.

Output

google.org. 154 IN A 216.239.32.27
google.org. 154 IN AAAA 2001:4860:4802:32::1b
google.org. 300 IN MX 0 smtp.google.com.
google.org. 172800 IN NS ns4.google.com.
google.org. 172800 IN NS ns1.google.com.
google.org. 172800 IN NS ns3.google.com.
google.org. 172800 IN NS ns2.google.com.
google.org. 3600 IN TXT "v=spf1 include:_spf.google.com ~all"
google.org. 60 IN SOA ns1.google.com. dns-admin.google.com. 387768352 900 900 1800 60

Different ways to get an IP address from the website using Python

Code 1

import socket
def get_IP():
    domain_name= 'www.google.com'
    try:
        print("IP Address for " + domain_name + " is " + socket.gethostbyname(domain_name))
    except socket.error as f:
        print("Error found" , f)
if __name__ == "__main__":
    get_IP()

Explanation

To get an IP address, we have a library in Python that is a socket. First, we are importing a library socket. Next to creating a get_IP function to pass the domain name. Here we are passing a website address of google to get its IP address. It displays the except block when it gets the wrong web address.

Output

IP Address for www.google.com is 172.217.163.196

Code 2

import socket
website=input("Enter the website name:")
full_web_url=f"www.{website}.com"
ip_add=socket.gethostbyname(full_web_url)
print(ip_add)

Explanation

First importing a library socket. Next getting the website name from the user. Using the formatted function to get the full web address. And then getting the IP address of the web address.

Output

Enter the website name:google
172.217.163.196

Code 3

import socket
def ddns():
    web_address = input ("Please enter website address(URL):")
    try:
        print(f"IP: {socket.gethostbyname(web_address)}")
    except socket.error as q:
        print("Invalid web address")
ddns()

Explanation

First importing a library socket. Next create a function named ddns(). And then getting the web address from the user. Using the formatted function to get the web address. Expect block is displayed if the invalid web address is given.

Output

Please enter website address(URL):www.google.com
IP: 172.217.163.196

1. Write the expansion of DDNS.

The expansion of DDNS is Dynamic Domain Name System or Dynamic Domain Name Server.

2. What is DDNS?

DDNS is useful to translate domain names to IP addresses. Basically when we type a domain name into the browser. It searches on the thousands of servers available and gives us an exact IP address. 

3. What is an IP address?

An IP address is nothing but a physical location on the internet.

4. Which library in python is used to get the IP address?

To get an IP address, we have a library in Python that is the socket.

5. Write a real-time example for IP address?

If someone wants to send us a letter then he/she should have our home address. Similarly, our computer also needs an address so that computers on the internet can send us a file that we want to download is called an IP address.

Conclusion

Here we have seen DDNS in python. And we saw how to get the IP address using python. The above-shown is just an example of using many ways of code to convert the web name into an IP address using socket library.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments