server.py), which provides a complete REST API for managing multiple ZKTeco biometric devices dynamically through API endpoints.
Overview
The multi-device server is designed for:- Organizations with multiple biometric devices across different locations
- Dynamic device management via REST API
- Centralized attendance tracking from all devices
- Automatic persistence of device configurations
devices.json.
Key Features
- Dynamic Device Management: Add/edit/remove devices via API without restarting
- Persistent Storage: Device configurations saved automatically to disk
- Parallel Data Retrieval: Query all devices simultaneously for faster responses
- Thread-Safe Operations: Concurrent request handling with device-level locking
- Default Device Support: Optional pre-configured devices on first startup
Prerequisites
Environment Variables
Configure the server before starting:| Variable | Default | Description |
|---|---|---|
API_HOST | 0.0.0.0 | Server listening interface |
API_PORT | 5000 | Server port |
CERT_FILE | cert.pem | SSL certificate file path |
KEY_FILE | key.pem | SSL private key file path |
DEVICES_FILE | devices.json | Device registry persistence file |
Starting the Server
- First Startup
- With Custom Configuration
- Production (systemd)
- Docker Compose
On first run, the server creates a default device configuration:Default device created:This device serves as an example and can be deleted or modified via the API.
Device Management
Registering Devices
Add devices dynamically via the API:Testing Device Connectivity
Verify each device is reachable:Updating Device Configuration
Modify device settings without removing and re-adding:All fields are optional. Only provided fields will be updated. The changes are immediately persisted to
devices.json.Removing Devices
devices.json is updated automatically.
Devices.json Persistence
The server automatically manages thedevices.json file:
File Structure
Persistence Behavior
Automatic Saves
Automatic Saves
The file is updated automatically on:
- Device registration (
POST /devices) - Device update (
PUT /devices/{id}) - Device deletion (
DELETE /devices/{id})
Startup Loading
Startup Loading
On server start:
- If
devices.jsonexists → Load all devices from file - If file doesn’t exist → Create with default device example
Manual Editing
Manual Editing
You can manually edit
devices.json while the server is stopped. On next startup, the server will load your changes.After editing:Backup and Migration
Backup and Migration
Backup your device configuration:Migrate to a new server:
Centralized Attendance Retrieval
Query attendance from all devices simultaneously:All Devices at Once
Data is retrieved in parallel from all devices using threading. If a device fails, its error is reported in the
errors object, but successful devices still return data.Filtering Across All Devices
Single Device Attendance
Per-Device Operations
All device operations use the pattern/devices/{device_id}/{operation}:
Device Information
User Management
List Users on Device
List Users on Device
Create User on Device
Create User on Device
Update User on Device
Update User on Device
Delete User from Device
Delete User from Device
Fingerprint Management
Time Synchronization
Multi-Device Deployment Patterns
Distributed Locations
High Availability Setup
Use Load Balancer
Deploy multiple server instances behind a load balancer, sharing the same
devices.json via NFS or syncing mechanism.Thread Safety and Concurrency
The server implements device-level locking:- Multiple devices can be queried simultaneously
- Prevents device conflicts and corruption
- Single device requests are serialized automatically
- Request A to device “entrance” - starts immediately
- Request B to device “warehouse” - starts immediately (different device)
- Request C to device “entrance” - waits for Request A to finish
Troubleshooting
Device Not Found Error
Device Not Found Error
Error:
Dispositivo 'xxx' no encontradoSolutions:- List devices:
curl http://localhost:5000/devices - Verify device ID matches exactly (case-sensitive)
- Check if device was deleted
Duplicate Device ID
Duplicate Device ID
Error:
El id 'xxx' ya existeSolutions:- Use a different unique identifier
- Update the existing device instead (
PUT /devices/{id}) - Delete the old device first if replacing it
devices.json Permission Errors
devices.json Permission Errors
Error:
Permission denied when writing devices.jsonSolutions:Partial Failure in /attendance/all
Partial Failure in /attendance/all
Some devices succeed, others fail in the
errors object.Solutions:- Check device connectivity individually with
/devices/{id}/ping - Review device-specific error messages in response
- Verify network routes to failed devices
- Check if failed devices are powered on
devices.json Corruption
devices.json Corruption
Error: JSON parsing error on startupSolutions:
Performance Optimization
Large Deployments (10+ Devices)
Increase Timeout
For slow networks or distant devices:
Use Filtering
Reduce data transfer with date/user filters:
Monitor Resource Usage
Each device query spawns a thread. Monitor with:
Implement Caching
For frequently accessed data, implement Redis or memcached caching layer
Migration from Single to Multi-Device
Upgrading fromservidor.py to server.py:
Next Steps
SSL Configuration
Secure your multi-device deployment with HTTPS
Environment Variables
Complete configuration reference
API Reference
Full endpoint documentation
Device Management API
Detailed device CRUD operations