A robust and enterprise-ready mDNS (multicast DNS) communication system for master-client device discovery and monitoring in local networks. This system provides automatic service discovery, health monitoring, and network resilience with sophisticated error recovery.
- Automatic Service Discovery: Zero-configuration device discovery using mDNS/Bonjour
- Master-Client Architecture: Scalable architecture supporting one master and multiple clients
- Health Monitoring: Advanced mDNS service presence monitoring with configurable timeouts
- Network Resilience: Automatic interface monitoring and service re-registration
- Smart Reconnection: Exponential backoff reconnection with automatic master rediscovery
- Interface Prioritization: Configurable network interface preferences (Ethernet over WiFi)
- Graceful Shutdown: Proper cleanup and service unregistration
- Comprehensive Logging: Structured logging with file and console output
- Configuration Management: JSON-based configuration with validation
- Cross-Platform: Works on Linux, macOS, and Windows
- Python 3.8 or higher
- Network interfaces supporting multicast (mDNS)
- Administrative privileges may be required on some systems
# Clone the repository
git clone https://github.com/sithulaka/mdns_device_discovery.git
cd mdns_device_discovery
# Create virtual environment (recommended)
python3 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt# Create sample configuration files
python3 main.py --create-configs
# Test the installation
python3 main.py --help# Quick start as master
python3 main.py --master
# Or with custom configuration
python3 main.py --config config_master.json# Quick start as client
python3 main.py --client
# Or with custom configuration
python3 main.py --config config_client.json# Run with specific config file
python3 main.py --config my_custom_config.json
# Enable verbose logging
python3 main.py --client --verbose{
"client_id": "master_device",
"is_master": true,
"port": 5000,
"master_ip": null,
"network_preferences": {
"allow_wifi": false,
"prefer_ethernet": true,
"interface_priority": ["eth", "en", "wlan", "wlp"]
},
"timeouts": {
"master_discovery_timeout": 30,
"master_wait_timeout": 60,
"health_check_interval": 10,
"network_check_interval": 5
},
"health_check": {
"enabled": true,
"max_failures": 3,
"timeout": 3,
"service_timeout": 30
}
}| Parameter | Description | Default |
|---|---|---|
client_id |
Unique device identifier | "device_001" |
is_master |
Whether device acts as master | false |
port |
Service port number | 5000 |
allow_wifi |
Allow WiFi interfaces | true |
prefer_ethernet |
Prefer Ethernet over WiFi | true |
health_check_interval |
Health check frequency (seconds) | 10 |
max_failures |
Max consecutive failures before disconnect | 3 |
Terminal 1 (Master Device):
python3 main.py --masterTerminal 2 (Client Device):
python3 main.py --clientfrom mdns_module import MDNSCommunicator
# Initialize communicator
communicator = MDNSCommunicator("config.json")
# Start the service
communicator.start()
# Check device type
if communicator.is_master_device():
print("Running as master device")
else:
print("Running as client device")
master_ip = communicator.get_master_ip()
if master_ip:
print(f"Connected to master at {master_ip}")
# Get connection status
status = communicator.get_connection_status()
print(f"Status: {status}")
# Cleanup
communicator.stop()βββββββββββββββββββ βββββββββββββββββββ
β Master Device β β Client Device β
β β β β
β mDNS Service βββββΊβ mDNS Discovery β
β Registration β β Service Browser β
β β β β
β Network β β Health Monitor β
β Monitoring β β Reconnection β
βββββββββββββββββββ βββββββββββββββββββ
MDNSDevice: Core device management and service registrationMDNSCommunicator: High-level API for application integrationHealthMonitor: mDNS service presence monitoringNetworkManager: Network interface detection and monitoringReconnectionManager: Automatic reconnection with exponential backoff
# Clone and setup
git clone https://github.com/sithulaka/mdns_device_discovery.git
cd mdns_device_discovery
# Create virtual environment
python3 -m venv venv
source venv/bin/activate
# Install development dependencies
pip install -r requirements-dev.txt
# Install pre-commit hooks
pre-commit install# Run all tests
python -m pytest
# Run with coverage
python -m pytest --cov=mdns_system
# Run specific test
python -m pytest tests/test_mdns_module.py# Format code
black .
# Check formatting
black --check .
# Sort imports
isort .The system provides comprehensive logging at multiple levels:
- Console Output: Real-time status updates
- File Logging: Detailed logs saved to
mdns_communication.log - Debug Mode: Verbose logging with
--verboseflag
INFO: Normal operation statusWARNING: Recoverable issues (network changes, reconnections)ERROR: Serious issues requiring attentionDEBUG: Detailed diagnostic information
- Ethernet: Primary preference for stability
- WiFi: Supported with configuration option
- Virtual Interfaces: Docker, VPN interfaces are filtered out
- Multicast support on network infrastructure
- Port 5353 (mDNS) must be open
- Same network segment for device discovery
Ensure the following ports are open:
- UDP 5353: mDNS service discovery
- TCP 5000: Application service port (configurable)
# Check network connectivity
ping <master_ip>
# Verify mDNS is working
avahi-browse -rt _master-device._tcp # Linux
dns-sd -B _master-device._tcp # macOS# Check interface status
ip addr show # Linux
ifconfig # macOS/Unix
# Verify permissions
sudo python3 main.py --master # Try with elevated privileges# Test mDNS resolution
nslookup master-device.local
# Check service advertisement
avahi-resolve -n master-device.local # LinuxEnable verbose logging for detailed troubleshooting:
python3 main.py --client --verboseWe welcome contributions! Please see our Contributing Guidelines for details.
- Fork the repository
- Create a feature branch:
git checkout -b feature/amazing-feature - Commit changes:
git commit -m 'Add amazing feature' - Push to branch:
git push origin feature/amazing-feature - Open a Pull Request
- Follow PEP 8 style guidelines
- Add type hints to all functions
- Include docstrings for all public methods
- Write tests for new features
- Update documentation as needed
This project is licensed under the MIT License - see the LICENSE file for details.
- Setup Instructions - Detailed setup guide
- API Documentation - Complete API reference
- Examples - Usage examples and tutorials
- Issues: GitHub Issues
- Discussions: GitHub Discussions
- Documentation: Wiki
- python-zeroconf - mDNS implementation
- psutil - System and network utilities
Made with β€οΈ by sithulaka