Added receiver example

This commit is contained in:
Pünkösd Marcell 2022-01-31 22:27:58 +01:00
parent 2d691e2bb5
commit 6dccd890c8

View File

@ -14,7 +14,7 @@ These information are the following:
The messages is formed as the following UTF-8 encoded string: The messages is formed as the following UTF-8 encoded string:
``` ```
BIRBOX|{ipaddr}|{hostname}|{version}|{uptime} BIRBOX|{ipaddr}|{hostname}|{version}|{uptime in seconds}
``` ```
The messages are broadcast to the 6969 UDP port. The messages are broadcast to the 6969 UDP port.
@ -24,3 +24,31 @@ All you need is to listen to such packets on the network, so you can find your l
## Usage ## Usage
Just start the script as a system daemon. You may have to change the interface name. Just start the script as a system daemon. You may have to change the interface name.
## Receiver example
```python3
import socket
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
client.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
client.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
client.bind(("", 6969))
while True:
data, addr = client.recvfrom(1500)
src_ip = addr[0]
try:
header, self_ip, hostname, version, uptime = data.decode().split('|')
except ValueError:
continue
if header == 'BIRBOX':
print("IP Address:",src_ip)
if src_ip != self_ip:
print("Warning! Reported and origin IP differ! Reported ip:",self._ip)
print("Hostname:", hostname)
print("Version:", version)
print("System uptime:", uptime,'sec')
```