-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
51 lines (40 loc) · 1.31 KB
/
client.py
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
39
40
41
42
43
44
45
46
47
48
49
50
51
# -*- coding: utf-8 -*-
"""
Copyright (c) 2023 Miguel Massot Campos
All rights reserved.
Licensed under the BSD 3-Clause License.
See LICENSE.md file in the project root for full license information.
"""
import socket
from threading import Thread
import json
class Listener:
def __init__(self):
# -- UDP
self.client = socket.socket(
socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP
)
# -- Enable port reusage
self.client.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# -- Enable broadcasting mode
self.client.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
# self.client.settimeout(params["timeout"])
self.client.bind(("", 50000))
self.th = Thread(target=self.loop, daemon=True)
self.th.start()
# -- data recieved
self.data = None
def loop(self):
while True:
print("waiting for data...")
try:
broadcast_data, _ = self.client.recvfrom(4096)
print("received message:", json.loads(broadcast_data))
except Exception as e:
print("Got exception trying to recv %s" % e)
def __del__(self):
self.client.close()
if __name__ == "__main__":
listener = Listener()
while True:
pass