Run linter

This commit is contained in:
Santiago Lo Coco 2024-11-05 19:39:20 +01:00
parent 97b8682818
commit 025fba31ff
5 changed files with 58 additions and 42 deletions

View File

@ -1,6 +1,7 @@
from zeroconf import Zeroconf, ServiceInfo
import socket
from zeroconf import ServiceInfo, Zeroconf
service_type = "_http._tcp.local."
service_name = "RoomF2703._http._tcp.local."
server_ip = "192.168.137.1"
@ -14,7 +15,7 @@ info = ServiceInfo(
addresses=[ip_address],
server="RoomF2703.local.",
port=server_port,
properties={"path": "/api/endpoints"}
properties={"path": "/api/endpoints"},
)
zeroconf.register_service(info)

View File

@ -1,23 +1,17 @@
from zeroconf import Zeroconf, ServiceInfo, ServiceBrowser
import socket
import threading
import time
from zeroconf import ServiceInfo, Zeroconf
service_type = "_http._tcp.local."
server_ip = "192.168.137.1"
server_port = 5000
room_services = [
"room2704",
"room2705",
"room2706",
"room2707",
"room2708"
]
room_services = ["room2704", "room2705", "room2706", "room2707", "room2708"]
zeroconf = Zeroconf()
ip_address = socket.inet_aton(server_ip)
service_infos = []
def register_services():
for room in room_services:
info = ServiceInfo(
@ -26,12 +20,13 @@ def register_services():
addresses=[ip_address],
server=f"{room}.local.",
port=server_port,
properties={"path": "/api/endpoints"}
properties={"path": "/api/endpoints"},
)
zeroconf.register_service(info)
service_infos.append(info)
print(f"Service {room} is now discoverable on {server_ip}:{server_port}")
if __name__ == "__main__":
register_services()

View File

@ -1,9 +1,16 @@
import socket
import struct
import time
def parse_mdns_response(data):
transaction_id, flags, questions, answer_rrs, authority_rrs, additional_rrs = struct.unpack('>HHHHHH', data[:12])
(
transaction_id,
flags,
questions,
answer_rrs,
authority_rrs,
additional_rrs,
) = struct.unpack(">HHHHHH", data[:12])
offset = 12
for _ in range(questions):
@ -16,10 +23,13 @@ def parse_mdns_response(data):
for _ in range(additional_rrs):
offset = parse_record(data, offset)
def parse_record(data, offset):
name, offset = read_name(data, offset)
record_type, record_class, ttl, data_length = struct.unpack('>HHIH', data[offset:offset + 10])
record_type, record_class, ttl, data_length = struct.unpack(
">HHIH", data[offset : offset + 10]
)
offset += 10
if ttl == 0:
@ -32,12 +42,14 @@ def parse_record(data, offset):
ip_address = socket.inet_ntoa(data[offset : offset + data_length])
print(f"A Record: {name} -> {ip_address}")
elif record_type == 33: # SRV
priority, weight, port = struct.unpack('>HHH', data[offset:offset + 6])
priority, weight, port = struct.unpack(">HHH", data[offset : offset + 6])
target, _ = read_name(data, offset + 6)
print(f"SRV Record: {name} -> {target}:{port} (priority: {priority}, weight: {weight})")
print(
f"SRV Record: {name} -> {target}:{port} (priority: {priority}, weight: {weight})"
)
elif record_type == 16: # TXT
print(data_length)
txt_data = data[offset:offset + data_length].decode('utf-8')
txt_data = data[offset : offset + data_length].decode("utf-8")
print(f"TXT Record: {name} -> {txt_data}")
elif record_type == 47: # NSEC
next_domain, _ = read_name(data, offset)
@ -48,6 +60,7 @@ def parse_record(data, offset):
return offset + data_length
def read_name(data, offset):
labels = []
original_offset = offset
@ -60,7 +73,7 @@ def read_name(data, offset):
if not jumped:
original_offset = offset + 2
pointer = struct.unpack('>H', data[offset:offset + 2])[0] & 0x3FFF
pointer = struct.unpack(">H", data[offset : offset + 2])[0] & 0x3FFF
offset = pointer
jumped = True
elif length == 0:
@ -68,13 +81,16 @@ def read_name(data, offset):
break
else:
offset += 1
labels.append(data[offset:offset + length].decode('utf-8', errors='ignore'))
labels.append(
data[offset : offset + length].decode("utf-8", errors="ignore")
)
offset += length
if jumped:
return '.'.join(labels), original_offset
return ".".join(labels), original_offset
else:
return '.'.join(labels), offset
return ".".join(labels), offset
def skip_name(data, offset):
while data[offset] != 0:
@ -95,23 +111,25 @@ def send_mdns_query(service_type):
sock.setsockopt(
socket.IPPROTO_IP,
socket.IP_ADD_MEMBERSHIP,
struct.pack("=4s4s", socket.inet_aton(multicast_address), socket.inet_aton(interface_ip))
struct.pack(
"=4s4s", socket.inet_aton(multicast_address), socket.inet_aton(interface_ip)
),
)
def create_query():
query = bytearray()
query += b'\x00\x00'
query += b'\x00\x00'
query += b'\x00\x01'
query += b'\x00\x00'
query += b'\x00\x00'
query += b'\x00\x00'
query += b"\x00\x00"
query += b"\x00\x00"
query += b"\x00\x01"
query += b"\x00\x00"
query += b"\x00\x00"
query += b"\x00\x00"
for part in service_type.split('.'):
for part in service_type.split("."):
query += bytes([len(part)]) + part.encode()
query += b'\x00'
query += b'\x00\x0C'
query += b'\x00\x01'
query += b"\x00"
query += b"\x00\x0C"
query += b"\x00\x01"
return query
@ -124,7 +142,7 @@ def send_mdns_query(service_type):
data, addr = sock.recvfrom(1024)
print(f"Received response from {addr[0]}")
flags = struct.unpack('>H', data[2:4])[0]
flags = struct.unpack(">H", data[2:4])[0]
if flags == 0:
print("Ignoring non-response packet")
continue
@ -135,5 +153,6 @@ def send_mdns_query(service_type):
finally:
sock.close()
if __name__ == "__main__":
send_mdns_query("_http._tcp.local")

View File

@ -1,2 +1,3 @@
[flake8]
max-line-length = 110
ignore = E203