-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwo_devices.py
More file actions
38 lines (31 loc) · 891 Bytes
/
two_devices.py
File metadata and controls
38 lines (31 loc) · 891 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import time
from easyhttp_python import EasyHTTP
# Callback function on FETCH
def handle_fetch(sender_id, query, timestamp):
print(f"FETCH request from {sender_id} on {timestamp} with {query}")
return {
"response": "Hello EasyHTTP!"
}
# Callback function on DATA
def handle_data(sender_id, data):
print(f"Received from {sender_id}: {data}")
def main():
# Initializing
easy = EasyHTTP(debug=True, port=5000)
# Setting up callback functions
easy.on("on_fetch", handle_fetch)
easy.on("on_data", handle_data)
# Starting
easy.start()
# Getting data by ID once every 3 seconds
try:
while True:
easy.fetch("ABC123")
time.sleep(3)
except KeyboardInterrupt:
# Gracefully stopping API
easy.stop()
except Exception as e:
print(e)
if __name__ == '__main__':
main()