Skip to main content

A simple distributed hash table implementation

Project description

Distributed Hash Table (DHT) Implementation

A Python-based Distributed Hash Table implementation that allows nodes to connect across different networks using IP addresses. This implementation supports key-value storage and retrieval across multiple nodes.

Features

  • Cross-network node communication
  • Key-value storage and retrieval
  • Automatic node discovery
  • Data replication between nodes
  • Simple CLI interface
  • Public IP detection
  • Local network support

Installation

From PyPI (Recommended)

pip install simpledht

From Source

  1. Clone the repository:
git clone <repository-url>
cd SimpleDHT
  1. Create and activate a virtual environment:
python -m venv env
source env/bin/activate  # On Windows: env\Scripts\activate
  1. Install the package in development mode:
pip install -e .

Usage

Starting a Node

To start a new DHT node:

python cli.py start --host 0.0.0.0 --port 5000

To start a node and connect to existing nodes:

python cli.py start --host 0.0.0.0 --port 5001 --bootstrap "PUBLIC_IP:5000"

Storing Data

To store a key-value pair:

python cli.py put --host PUBLIC_IP --port 5000 mykey "my value"

Retrieving Data

To retrieve a value:

python cli.py get --host PUBLIC_IP --port 5000 mykey

Cross-Network Example

  1. Start Node 1 (First network):
python cli.py start --host 0.0.0.0 --port 5000
  1. Start Node 2 (Second network):
python cli.py start --host 0.0.0.0 --port 5000 --bootstrap "NODE1_PUBLIC_IP:5000"
  1. Store and retrieve data:
# Store on Node 1
python cli.py put --host NODE1_PUBLIC_IP --port 5000 test_key "test_value"

# Retrieve from Node 2
python cli.py get --host NODE2_PUBLIC_IP --port 5000 test_key

Network Configuration

Firewall Setup

Ensure the UDP port (default: 5000) is open in your firewall:

# For UFW (Ubuntu)
sudo ufw allow 5000/udp

# For iptables
sudo iptables -A INPUT -p udp --dport 5000 -j ACCEPT

Port Forwarding

If your node is behind a NAT router:

  1. Access your router's admin interface
  2. Set up port forwarding for UDP port 5000
  3. Forward to your node's local IP address

Troubleshooting

Common Issues

  1. Connection Timeout

    • Check if the target node is running
    • Verify firewall settings
    • Ensure port forwarding is configured correctly
    • Try increasing the timeout: --timeout 10
  2. Address Already in Use

    • The port is already being used by another process
    • Try a different port number
    • Check running processes: netstat -tuln | grep 5000
  3. No Response from Node

    • Verify the node is running
    • Check network connectivity: ping NODE_IP
    • Test port connectivity: nc -vzu NODE_IP 5000

Error Messages

  • Failed to bootstrap with IP:PORT: Invalid bootstrap node format
  • No response received: Node is not responding
  • Address already in use: Port conflict
  • Failed to get public IP: Network connectivity issue

Architecture

The DHT implementation uses:

  • UDP sockets for communication
  • SHA-256 for node ID generation
  • Automatic public IP detection
  • Data replication between nodes
  • Bootstrap nodes for network discovery

Security Considerations

  • This is a basic implementation and should not be used in production without additional security measures
  • Consider adding:
    • Encryption for data in transit
    • Authentication for node joining
    • Rate limiting to prevent abuse
    • Input validation

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Commit your changes
  4. Push to the branch
  5. Create a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

simpledht-0.1.0.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

simpledht-0.1.0-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file simpledht-0.1.0.tar.gz.

File metadata

  • Download URL: simpledht-0.1.0.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for simpledht-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e587b4f801300ee9f98b8629d8bf8c11039c3bbac5550ac89f6b91426ea9049b
MD5 2ab5796bfee4b0cb537f06a958289b17
BLAKE2b-256 d7e775fe00b7e689d4415436c00035aa903c9ce31cdf21cdfe7d73f045a436f0

See more details on using hashes here.

File details

Details for the file simpledht-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: simpledht-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for simpledht-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b816539071d741e80ea8466c79325c7e208b878083ee0d1427d8a080aede4d10
MD5 835df2e20d18ccdbb0cd7dd50fd2a11d
BLAKE2b-256 e27e405c540bd6617e5fe2b17f501351694291fec3186068d8bb7cab4a0d58dc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page