Run linter
This commit is contained in:
parent
97b8682818
commit
025fba31ff
|
@ -1,6 +1,7 @@
|
||||||
from zeroconf import Zeroconf, ServiceInfo
|
|
||||||
import socket
|
import socket
|
||||||
|
|
||||||
|
from zeroconf import ServiceInfo, Zeroconf
|
||||||
|
|
||||||
service_type = "_http._tcp.local."
|
service_type = "_http._tcp.local."
|
||||||
service_name = "RoomF2703._http._tcp.local."
|
service_name = "RoomF2703._http._tcp.local."
|
||||||
server_ip = "192.168.137.1"
|
server_ip = "192.168.137.1"
|
||||||
|
@ -14,7 +15,7 @@ info = ServiceInfo(
|
||||||
addresses=[ip_address],
|
addresses=[ip_address],
|
||||||
server="RoomF2703.local.",
|
server="RoomF2703.local.",
|
||||||
port=server_port,
|
port=server_port,
|
||||||
properties={"path": "/api/endpoints"}
|
properties={"path": "/api/endpoints"},
|
||||||
)
|
)
|
||||||
|
|
||||||
zeroconf.register_service(info)
|
zeroconf.register_service(info)
|
||||||
|
|
|
@ -1,23 +1,17 @@
|
||||||
from zeroconf import Zeroconf, ServiceInfo, ServiceBrowser
|
|
||||||
import socket
|
import socket
|
||||||
import threading
|
|
||||||
import time
|
from zeroconf import ServiceInfo, Zeroconf
|
||||||
|
|
||||||
service_type = "_http._tcp.local."
|
service_type = "_http._tcp.local."
|
||||||
server_ip = "192.168.137.1"
|
server_ip = "192.168.137.1"
|
||||||
server_port = 5000
|
server_port = 5000
|
||||||
room_services = [
|
room_services = ["room2704", "room2705", "room2706", "room2707", "room2708"]
|
||||||
"room2704",
|
|
||||||
"room2705",
|
|
||||||
"room2706",
|
|
||||||
"room2707",
|
|
||||||
"room2708"
|
|
||||||
]
|
|
||||||
|
|
||||||
zeroconf = Zeroconf()
|
zeroconf = Zeroconf()
|
||||||
ip_address = socket.inet_aton(server_ip)
|
ip_address = socket.inet_aton(server_ip)
|
||||||
service_infos = []
|
service_infos = []
|
||||||
|
|
||||||
|
|
||||||
def register_services():
|
def register_services():
|
||||||
for room in room_services:
|
for room in room_services:
|
||||||
info = ServiceInfo(
|
info = ServiceInfo(
|
||||||
|
@ -26,12 +20,13 @@ def register_services():
|
||||||
addresses=[ip_address],
|
addresses=[ip_address],
|
||||||
server=f"{room}.local.",
|
server=f"{room}.local.",
|
||||||
port=server_port,
|
port=server_port,
|
||||||
properties={"path": "/api/endpoints"}
|
properties={"path": "/api/endpoints"},
|
||||||
)
|
)
|
||||||
zeroconf.register_service(info)
|
zeroconf.register_service(info)
|
||||||
service_infos.append(info)
|
service_infos.append(info)
|
||||||
print(f"Service {room} is now discoverable on {server_ip}:{server_port}")
|
print(f"Service {room} is now discoverable on {server_ip}:{server_port}")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
register_services()
|
register_services()
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,16 @@
|
||||||
import socket
|
import socket
|
||||||
import struct
|
import struct
|
||||||
import time
|
|
||||||
|
|
||||||
def parse_mdns_response(data):
|
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
|
offset = 12
|
||||||
|
|
||||||
for _ in range(questions):
|
for _ in range(questions):
|
||||||
|
@ -16,10 +23,13 @@ def parse_mdns_response(data):
|
||||||
for _ in range(additional_rrs):
|
for _ in range(additional_rrs):
|
||||||
offset = parse_record(data, offset)
|
offset = parse_record(data, offset)
|
||||||
|
|
||||||
|
|
||||||
def parse_record(data, offset):
|
def parse_record(data, offset):
|
||||||
name, offset = read_name(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
|
offset += 10
|
||||||
|
|
||||||
if ttl == 0:
|
if ttl == 0:
|
||||||
|
@ -29,15 +39,17 @@ def parse_record(data, offset):
|
||||||
target, _ = read_name(data, offset)
|
target, _ = read_name(data, offset)
|
||||||
print(f"PTR Record: {name} -> {target}")
|
print(f"PTR Record: {name} -> {target}")
|
||||||
elif record_type == 1: # A
|
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}")
|
print(f"A Record: {name} -> {ip_address}")
|
||||||
elif record_type == 33: # SRV
|
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)
|
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
|
elif record_type == 16: # TXT
|
||||||
print(data_length)
|
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}")
|
print(f"TXT Record: {name} -> {txt_data}")
|
||||||
elif record_type == 47: # NSEC
|
elif record_type == 47: # NSEC
|
||||||
next_domain, _ = read_name(data, offset)
|
next_domain, _ = read_name(data, offset)
|
||||||
|
@ -48,6 +60,7 @@ def parse_record(data, offset):
|
||||||
|
|
||||||
return offset + data_length
|
return offset + data_length
|
||||||
|
|
||||||
|
|
||||||
def read_name(data, offset):
|
def read_name(data, offset):
|
||||||
labels = []
|
labels = []
|
||||||
original_offset = offset
|
original_offset = offset
|
||||||
|
@ -60,7 +73,7 @@ def read_name(data, offset):
|
||||||
if not jumped:
|
if not jumped:
|
||||||
original_offset = offset + 2
|
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
|
offset = pointer
|
||||||
jumped = True
|
jumped = True
|
||||||
elif length == 0:
|
elif length == 0:
|
||||||
|
@ -68,13 +81,16 @@ def read_name(data, offset):
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
offset += 1
|
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
|
offset += length
|
||||||
|
|
||||||
if jumped:
|
if jumped:
|
||||||
return '.'.join(labels), original_offset
|
return ".".join(labels), original_offset
|
||||||
else:
|
else:
|
||||||
return '.'.join(labels), offset
|
return ".".join(labels), offset
|
||||||
|
|
||||||
|
|
||||||
def skip_name(data, offset):
|
def skip_name(data, offset):
|
||||||
while data[offset] != 0:
|
while data[offset] != 0:
|
||||||
|
@ -95,23 +111,25 @@ def send_mdns_query(service_type):
|
||||||
sock.setsockopt(
|
sock.setsockopt(
|
||||||
socket.IPPROTO_IP,
|
socket.IPPROTO_IP,
|
||||||
socket.IP_ADD_MEMBERSHIP,
|
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():
|
def create_query():
|
||||||
query = bytearray()
|
query = bytearray()
|
||||||
query += b'\x00\x00'
|
query += b"\x00\x00"
|
||||||
query += b'\x00\x00'
|
query += b"\x00\x00"
|
||||||
query += b'\x00\x01'
|
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\x00"
|
||||||
|
|
||||||
for part in service_type.split('.'):
|
for part in service_type.split("."):
|
||||||
query += bytes([len(part)]) + part.encode()
|
query += bytes([len(part)]) + part.encode()
|
||||||
query += b'\x00'
|
query += b"\x00"
|
||||||
query += b'\x00\x0C'
|
query += b"\x00\x0C"
|
||||||
query += b'\x00\x01'
|
query += b"\x00\x01"
|
||||||
|
|
||||||
return query
|
return query
|
||||||
|
|
||||||
|
@ -124,7 +142,7 @@ def send_mdns_query(service_type):
|
||||||
data, addr = sock.recvfrom(1024)
|
data, addr = sock.recvfrom(1024)
|
||||||
print(f"Received response from {addr[0]}")
|
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:
|
if flags == 0:
|
||||||
print("Ignoring non-response packet")
|
print("Ignoring non-response packet")
|
||||||
continue
|
continue
|
||||||
|
@ -135,5 +153,6 @@ def send_mdns_query(service_type):
|
||||||
finally:
|
finally:
|
||||||
sock.close()
|
sock.close()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
send_mdns_query("_http._tcp.local")
|
send_mdns_query("_http._tcp.local")
|
||||||
|
|
|
@ -1,2 +1,3 @@
|
||||||
[flake8]
|
[flake8]
|
||||||
max-line-length = 110
|
max-line-length = 110
|
||||||
|
ignore = E203
|
Loading…
Reference in New Issue