From 025fba31ff250de7a2058ab59eed327029a9be6f Mon Sep 17 00:00:00 2001 From: Santiago Lo Coco Date: Tue, 5 Nov 2024 19:39:20 +0100 Subject: [PATCH] Run linter --- src/broadcast.py | 5 +-- src/broadcast_multiple.py | 19 ++++------- src/client.py | 2 +- src/rfc_client.py | 71 +++++++++++++++++++++++++-------------- src/setup.cfg | 3 +- 5 files changed, 58 insertions(+), 42 deletions(-) diff --git a/src/broadcast.py b/src/broadcast.py index c316bea..0505c1f 100644 --- a/src/broadcast.py +++ b/src/broadcast.py @@ -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) diff --git a/src/broadcast_multiple.py b/src/broadcast_multiple.py index be98a3f..834396e 100644 --- a/src/broadcast_multiple.py +++ b/src/broadcast_multiple.py @@ -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,15 +20,16 @@ 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() - + try: input("Press enter to exit...\n\n") finally: diff --git a/src/client.py b/src/client.py index d919aa8..c361ee4 100644 --- a/src/client.py +++ b/src/client.py @@ -19,4 +19,4 @@ browser = ServiceBrowser(zeroconf, "_http._tcp.local.", listener) try: input("Press enter to exit...\n\n") finally: - zeroconf.close() \ No newline at end of file + zeroconf.close() diff --git a/src/rfc_client.py b/src/rfc_client.py index 432daf9..cc46bbd 100644 --- a/src/rfc_client.py +++ b/src/rfc_client.py @@ -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: @@ -29,15 +39,17 @@ def parse_record(data, offset): target, _ = read_name(data, offset) print(f"PTR Record: {name} -> {target}") elif record_type == 1: # A - ip_address = socket.inet_ntoa(data[offset:offset + data_length]) + 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: @@ -93,26 +109,28 @@ def send_mdns_query(service_type): sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.bind((interface_ip, multicast_port)) sock.setsockopt( - socket.IPPROTO_IP, - socket.IP_ADD_MEMBERSHIP, - struct.pack("=4s4s", socket.inet_aton(multicast_address), socket.inet_aton(interface_ip)) + socket.IPPROTO_IP, + socket.IP_ADD_MEMBERSHIP, + 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 query_packet = create_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") diff --git a/src/setup.cfg b/src/setup.cfg index fedd799..5f0a3ca 100644 --- a/src/setup.cfg +++ b/src/setup.cfg @@ -1,2 +1,3 @@ [flake8] -max-line-length = 110 \ No newline at end of file +max-line-length = 110 +ignore = E203 \ No newline at end of file