Dynamic DNS Made Easy: Building a Python-Based Solution

CK1820 
Created at Mar 15, 2024 19:04:34 
116   0   0   0  

Creating a dynamic DNS (Domain Name System) service in Python involves several components: a server that listens for DNS queries, a database to store mappings between domain names and IP addresses, and functionality to update these mappings dynamically. Below is a simple example of a Python-based dynamic DNS service using Flask, a lightweight web framework, and SQLite, a simple SQL database.

from flask import Flask, request, jsonify
import sqlite3

app = Flask(__name__)
DATABASE = 'dns.db'

def init_db():
    conn = sqlite3.connect(DATABASE)
    cursor = conn.cursor()
    cursor.execute('''
        CREATE TABLE IF NOT EXISTS dns_records (
            domain TEXT PRIMARY KEY,
            ip_address TEXT
        )
    ''')
    conn.commit()
    conn.close()

@app.route('/update_dns', methods=['POST'])
def update_dns():
    data = request.get_json()
    domain = data.get('domain')
    ip_address = data.get('ip_address')
    
    if not domain or not ip_address:
        return jsonify({'message': 'Domain and IP address are required.'}), 400
    
    conn = sqlite3.connect(DATABASE)
    cursor = conn.cursor()
    
    cursor.execute('INSERT OR REPLACE INTO dns_records (domain, ip_address) VALUES (?, ?)', (domain, ip_address))
    conn.commit()
    conn.close()
    
    return jsonify({'message': f'DNS record for {domain} updated successfully.'})

@app.route('/resolve_dns', methods=['GET'])
def resolve_dns():
    domain = request.args.get('domain')
    
    if not domain:
        return jsonify({'message': 'Domain is required.'}), 400
    
    conn = sqlite3.connect(DATABASE)
    cursor = conn.cursor()
    
    cursor.execute('SELECT ip_address FROM dns_records WHERE domain=?', (domain,))
    result = cursor.fetchone()
    conn.close()
    
    if result:
        return jsonify({'ip_address': result[0]})
    else:
        return jsonify({'message': 'Domain not found.'}), 404

if __name__ == '__main__':
    init_db()
    app.run(debug=True)

This code sets up a basic Flask web application with two endpoints:

  1. /update_dns: This endpoint allows clients to update DNS records by sending a POST request with JSON data containing the domain name and corresponding IP address.
  2. /resolve_dns: This endpoint resolves DNS queries by accepting a domain name as a query parameter and returning the associated IP address.

The DNS records are stored in an SQLite database named dns.db. The init_db() function initializes the database by creating a table named dns_records if it doesn't already exist.

To run this code:

  1. Install Flask (pip install Flask).
  2. Save the code in a file (e.g., dynamic_dns.py).
  3. Run the file (python dynamic_dns.py).
  4. The server will start, and you can use tools like cURL or Postman to send requests to the /update_dns and /resolve_dns endpoints.

Please note that this example is for educational purposes and lacks several features found in a production-grade dynamic DNS service, such as authentication, rate limiting, and error handling.



Tags: DNS Database Management Dynamic DNS Flask Networking Python SQLite Server Management Tutorial Web Development Share on Facebook Share on X

▶ NEXT
Implementing a Versatile DNS Server in Python: Handling A, AAAA, CNAME, and TXT Records
  Comments 0
Login for comment
SIMILAR POSTS

Building a Basic DNS Server in PHP/Python: A Beginner's Guide (updated at Mar 15, 2024)

Implementing a Versatile DNS Server in Python: Handling A, AAAA, CNAME, and TXT Records (created at Mar 16, 2024)

Exploring the Depths of Data Transfer: sendfile vs. kTLS (created at Mar 15, 2024)

Python Implementation of Linear Regression (updated at Apr 01, 2024)

Public DNS (Domain Name Service) based on IPv4, IPv6 widely used (updated at Feb 23, 2024)

Forecasting with Linear Regression and KNN Regression in Python (updated at Apr 07, 2024)

All Engineering Software Development How can you prioritize software design trade-offs when developing a new product? (created at Feb 21, 2024)

Understanding and Implementing K-Nearest Neighbors (KNN) Algorithm in Python (created at Apr 08, 2024)

Harnessing the Power of Random Forest Algorithm in Python (created at Apr 08, 2024)

Mastering Model Persistence: Saving and Loading Trained Machine Learning Models in Python (created at Apr 08, 2024)

Equal Height Blocks in Bootstrap with JavaScript (created at Apr 22, 2024)

Mastering Excel Data Importation in PHP (updated at Apr 24, 2024)

Creating a Pinterest-Style Card Layout with Bootstrap and Masonry (created at Apr 24, 2024)

Configuring IP Address on Centos 6.7 (created at Nov 08, 2015)

OTHER POSTS IN THE SAME CATEGORY

How do I determine the client IP type in Python - IPv4 or IPv6 (updated at Apr 13, 2024)

Predicting Buyer Preferences with PyTorch: A Deep Learning Approach (updated at Apr 09, 2024)

Forecasting the Weather with PyTorch: A Beginner's Guide to Temperature Prediction (created at Apr 09, 2024)

PyTorch example to Forcast Stock Price based on 10 days Dataset (created at Apr 09, 2024)

Getting Started with PyTorch: A Beginner's Guide to Building Your First Neural Network (updated at Apr 09, 2024)

Mastering Model Persistence: Saving and Loading Trained Machine Learning Models in Python (created at Apr 08, 2024)

Harnessing the Power of Random Forest Algorithm in Python (created at Apr 08, 2024)

Understanding and Implementing K-Nearest Neighbors (KNN) Algorithm in Python (created at Apr 08, 2024)

Forecasting with Linear Regression and KNN Regression in Python (updated at Apr 07, 2024)

Mastering Random Forest Regression: A Comprehensive Guide with Python Examples (updated at Apr 01, 2024)

Python Implementation of Linear Regression (updated at Apr 01, 2024)

Mastering Supervised Machine Learning with Python: A Comprehensive Guide (created at Apr 01, 2024)

Mastering AI: A Beginner's Guide to Python Programming and Beyond (created at Apr 01, 2024)

Implementing a Versatile DNS Server in Python: Handling A, AAAA, CNAME, and TXT Records (created at Mar 16, 2024)

UPDATES

Creating a Pinterest-Style Card Layout with Bootstrap and Masonry (created at Apr 24, 2024)

Mastering Excel Data Importation in PHP (updated at Apr 24, 2024)

JSON format control in PHP (updated at Apr 24, 2024)

Equal Height Blocks in Bootstrap with JavaScript (created at Apr 22, 2024)

How to convert integer to text string ? (updated at Apr 22, 2024)

Checking similarity between two strings in PHP (updated at Apr 21, 2024)

Create Blob Image in HTML based on the given Text, Width and Height in the Center of the Image without saving file (updated at Apr 21, 2024)

How do I determine the client IP type (IPv4/IPv6) in PHP (updated at Apr 16, 2024)

How do I determine the client IP type in Python - IPv4 or IPv6 (updated at Apr 13, 2024)

Getting Started with PyTorch: A Beginner's Guide to Building Your First Neural Network (updated at Apr 09, 2024)

Predicting Buyer Preferences with PyTorch: A Deep Learning Approach (updated at Apr 09, 2024)

Forecasting the Weather with PyTorch: A Beginner's Guide to Temperature Prediction (created at Apr 09, 2024)

PyTorch example to Forcast Stock Price based on 10 days Dataset (created at Apr 09, 2024)

Mastering Model Persistence: Saving and Loading Trained Machine Learning Models in Python (created at Apr 08, 2024)

Harnessing the Power of Random Forest Algorithm in Python (created at Apr 08, 2024)

Understanding and Implementing K-Nearest Neighbors (KNN) Algorithm in Python (created at Apr 08, 2024)

Forecasting with Linear Regression and KNN Regression in Python (updated at Apr 07, 2024)

What is 302 Found Redirection in HTTP 1.1? (created at Apr 04, 2024)

Mastering Random Forest Regression: A Comprehensive Guide with Python Examples (updated at Apr 01, 2024)

Python Implementation of Linear Regression (updated at Apr 01, 2024)

Mastering Supervised Machine Learning with Python: A Comprehensive Guide (created at Apr 01, 2024)

Mastering AI: A Beginner's Guide to Python Programming and Beyond (created at Apr 01, 2024)

How do I create animated background for Google Meet? (updated at Mar 28, 2024)

Building a Simple DNS Server in Delphi with TTL Support (created at Mar 16, 2024)

How to force cookies, disable php sessid in URL ? (updated at Mar 16, 2024)

Implementing a Versatile DNS Server in PHP: Handling A, AAAA, CNAME, and TXT Records (updated at Mar 16, 2024)

Implementing a Versatile DNS Server in Python: Handling A, AAAA, CNAME, and TXT Records (created at Mar 16, 2024)

Building a Basic DNS Server in PHP/Python: A Beginner's Guide (updated at Mar 15, 2024)

Exploring the Depths of Data Transfer: sendfile vs. kTLS (created at Mar 15, 2024)

How Netflix Ensures Smooth Streaming with Open Connect CDN (updated at Mar 15, 2024)