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
- Data synchronization when joining the network
- Reliable bootstrapping with retry mechanism
- Simple CLI interface
- Public IP detection
- Local network support
- Python library interface for programmatic use
Installation
From PyPI (Recommended)
pip install simpledht
From Source
- Clone the repository:
git clone <repository-url>
cd SimpleDHT
- Create and activate a virtual environment:
python -m venv env
source env/bin/activate # On Windows: env\Scripts\activate
- Install the package in development mode:
pip install -e .
Usage
As a Python Library
The package can be used programmatically in your Python code:
from simpledht import DHTNode
# Create and start a node
node = DHTNode(host='0.0.0.0', port=5000)
node.start()
# Store data
node.put('mykey', 'myvalue')
# Retrieve data
value = node.get('mykey')
# Connect to another node
node.bootstrap('other_node_ip:5000')
# Stop the node when done
node.stop()
See the examples/ directory for more detailed usage examples:
basic_usage.py: Simple example of creating and connecting nodesdistributed_storage.py: Advanced example showing distributed storage with multiple nodes
Command Line Interface
Starting a Node
To start a new DHT node:
simpledht start --host 0.0.0.0 --port 5000
To start a node and connect to existing nodes:
simpledht start --host 0.0.0.0 --port 5001 --bootstrap "PUBLIC_IP:5000"
Storing Data
To store a key-value pair:
simpledht put --host PUBLIC_IP --port 5000 mykey "my value"
Retrieving Data
To retrieve a value:
simpledht get --host PUBLIC_IP --port 5000 mykey
Cross-Network Example
- Start Node 1 (First network):
simpledht start --host 0.0.0.0 --port 5000
- Start Node 2 (Second network):
simpledht start --host 0.0.0.0 --port 5000 --bootstrap "NODE1_PUBLIC_IP:5000"
- Store and retrieve data:
# Store on Node 1
simpledht put --host NODE1_PUBLIC_IP --port 5000 test_key "test_value"
# Retrieve from Node 2
simpledht 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:
- Access your router's admin interface
- Set up port forwarding for UDP port 5000
- Forward to your node's local IP address
New Features in Version 0.1.2
- Improved Bootstrap Mechanism: Added retry logic for more reliable connections across networks
- Data Synchronization: Nodes automatically sync data when joining the network
- Enhanced Error Handling: Better handling of network timeouts and connection issues
- Full Data Replication: All nodes maintain a complete copy of the data for redundancy
Troubleshooting
Common Issues
-
Connection Timeout
- Check if the target node is running
- Verify firewall settings
- Ensure port forwarding is configured correctly
- Try increasing the timeout:
--timeout 10
-
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
-
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 formatNo response received: Node is not respondingAddress already in use: Port conflictFailed to get public IP: Network connectivity issueConnection attempt X/3 timed out, retrying...: Network latency or connectivity issues
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
- Retry mechanism for reliable connections
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
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file simpledht-0.1.3.tar.gz.
File metadata
- Download URL: simpledht-0.1.3.tar.gz
- Upload date:
- Size: 11.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
068ae65bf17bab80ae7bafde0737af391d3e4696ee231a5348c3e89e6f745964
|
|
| MD5 |
d7075486a11e7ee400329cd338ee5123
|
|
| BLAKE2b-256 |
421d56163a97009b0da63fa40ae5cfab66edc1aa22530e2b675fdff9745467a5
|
File details
Details for the file simpledht-0.1.3-py3-none-any.whl.
File metadata
- Download URL: simpledht-0.1.3-py3-none-any.whl
- Upload date:
- Size: 11.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
64e7bbe61978dfe93ef68f15378964cdb154d86c7687389d9bd676ec7eef860d
|
|
| MD5 |
6fcd4a6bb13e74776ab70db81b25c18f
|
|
| BLAKE2b-256 |
2b9a3c841f63802aa56e23854bc84457e9cbcf3a31e808d718f83927671d8b9d
|