Skip to content

Repository files navigation

EasyHTTP

EN README | RU README

A lightweight HTTP-based P2P framework for IoT and device-to-device communication

Protocol VersionDevelopment StatusLicensePython

Warning

Breaking Changes from 0.3.2

API Changes

# 0.3.2 (OLD)fromeasyhttpimport ...
# 0.3.3 - newer (NEW)fromeasyhttp_pythonimport ...

🚀 Quick Start

Installation

# Install by PyPI
pip install easyhttp-python
# Or from GitHub
pip install git+https://github.com/slpuk/easyhttp-python.git

Basic Usage (synchronous)

Syntax with context managers and full code is supported

fromeasyhttp_pythonimportEasyHTTPdefmain():
# Initialize a device with context managerwithEasyHTTP(debug=True, port=5000) aseasy:
print(f"Device ID: {easy.id}")
# Ping to check if device is onlineifeasy.ping("ABC123"):
print("Device is online!")
# Request data from deviceresponse=easy.fetch("ABC123")
ifresponse:
print(f"Received: {response.get('data')}")
# Push data to devicesuccess=easy.push("ABC123", {"led": "on"})
ifsuccess:
print("Command executed successfully")
# Starting main processif__name__=="__main__":
main()

Or asynchronous

importasynciofromeasyhttp_pythonimportEasyHTTPAsyncasyncdefmain():
# Initialize a deviceeasy=EasyHTTPAsync(debug=True, port=5000)
awaiteasy.start()
print(f"Device ID: {easy.id}")
# Ping to check if device is onlineifawaiteasy.ping("ABC123"):
print("Device is online!")
# Request data from deviceresponse=awaiteasy.fetch("ABC123")
ifresponse:
print(f"Received: {response.get('data')}")
# Push data to devicesuccess=awaiteasy.push("ABC123", {"led": "on"})
ifsuccess:
print("Command executed successfully")
# Starting main processif__name__=="__main__":
asyncio.run(main())

📖 About

EasyHTTP is a simple yet powerful framework with asynchronous core that enables P2P (peer-to-peer) communication between devices using plain HTTP.

Key Features:

  • 🔄 P2P Architecture - No central server required
  • 🧩 Dual API:EasyHTTP (synchronous) and EasyHTTPAsync (asynchronous) with the same methods
  • 📡 Event-Driven Communication - Callback-based architecture
  • 🆔 Human-Readable Device IDs - Base32 identifiers instead of IP addresses
  • ✅ Easy to Use - Simple API with minimal setup
  • 🚀 Performance - Asynchronous code and lightweight libraries(FastAPI/aiohttp)
  • ⚙️ Auto-detect - Devices automatically find each other

Project Structure

easyhttp-python/
├── docs/
│ ├── EasyHTTP.md # Sync API reference
│ └── EasyHTTPAsync.md # Async API reference
├── easyhttp_python/
│ ├── __init__.py
│ ├── core.py # Main framework file/core
│ ├── discovery.py # Discovery module
│ └── wrapper.py # Synchronous wrapper
├── examples/
│ ├── async/ # Asynchronous examples
│ │ ├── basic_ping.py
│ │ ├── callback_preview.py
│ │ ├── device_control.py
│ │ ├── sensor_simulator.py
│ │ └── two_devices.py
│ └── sync/ # Synchronous examples
│ ├── basic_ping.py
│ ├── callback_preview.py
│ ├── device_control.py
│ ├── sensor_simulator.py
│ └── two_devices.py
├── .gitignore
├── LICENSE # MIT license
├── pyproject.toml # Project config
├── README_PY.md # Documentation for PyPI
├── README_RU.md # Russian documentation
├── README.md # This file
└── requirements.txt # Project dependencies

🏗️ Architecture

Device Identification

Instead of using hard-to-remember IP addresses, each device in the EasyHTTP network has a unique 6-character identifier:

  • Format: 6 characters from Base32 alphabet (without ambiguous characters)
  • Alphabet: 23456789ABCDEFGHJKLMNPQRSTUVWXYZ
  • Examples: 7H8G2K, AB3F9Z, X4R7T2
  • Generation: Randomly generated on first boot, stored in device configuration

Command System

EasyHTTP uses a simple JSON-based command system:

CommandValueDescription
PING1Check if another device is reachable
PONG2Response to ping request
FETCH3Request data from a device
DATA4Send data or answer to FETCH
PUSH5Request to write/execute on remote device
ACK6Success/confirmation
NACK7Error/reject

Communication Flow

sequenceDiagram
participant DeviceA
participant DeviceB
DeviceA->>DeviceB: PING
DeviceB-->>DeviceA: PONG
DeviceA->>DeviceB: FETCH
DeviceB-->>DeviceA: DATA
DeviceA->>DeviceB: PUSH
DeviceB-->>DeviceA: ACK/NACK
Loading

📦 Installation & Setup

Installation

# Install from PyPI
pip install easyhttp-python
# Or from GitHub
pip install git+https://github.com/slpuk/easyhttp-python.git

Basic Example with Callbacks(Synchronous)

importtimefromeasyhttp_pythonimportEasyHTTP# Callback functiondefhandle_data(sender_id, data, timestamp):
# Callback for incoming DATA responsesprint(f"From {sender_id}: {data}")
defhandle_fetch(sender_id, query, timestamp):
# Callback for FETCH requests - returns data when someone requests itprint(f"FETCH request from {sender_id}")
return {
"temperature": 23.5,
"humidity": 45,
"status": "normal",
"timestamp": timestamp
}
defhandle_push(sender_id, data, timestamp):
# Callback for PUSH requests - handle control commandsprint(f"Control from {sender_id}: {data}")
ifdataanddata.get("command") =="led":
state=data.get("state", "off")
print(f"[CONTROL] Turning LED {state}")
# Here you can add real GPIO controlreturnTrue# Successful → ACKreturnFalse# Error → NACKdefmain():
# Initializing EasyHTTP - sync wrapper of EasyHTTPAsynceasy=EasyHTTP(debug=True, port=5000)
# Setting up callback functionseasy.on('on_ping', handle_ping)
easy.on('on_pong', handle_pong)
easy.on('on_fetch', handle_fetch)
easy.on('on_data', handle_data)
easy.on('on_push', handle_push)
easy.start() # Starting serverprint(f"Device {easy.id} is running on port 5000!")
# Adding deviceeasy.add("ABC123", "192.168.1.100", 5000)
print("Added device ABC123")
# Monitoring device's statustry:
whileTrue:
ifeasy.ping("ABC123"):
print("Device ABC123 is online")
else:
print("Device ABC123 is offline")
time.sleep(5)
exceptKeyboardInterrupt:
print("\nStopping device...")
easy.stop() # Stopping server# Starting main processif__name__=="__main__":
main()

📚 Examples

Check the examples/ directory for more:
(synchronous examples below; check examples/async/ for asynchronous versions)

🔧 API Reference

Check the directories for functions documentation:

About

A lightweight HTTP-based P2P framework for IoT and device-to-device communication

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages