Skip to content

Commit

Permalink
[Netlink] handle libudev msgs
Browse files Browse the repository at this point in the history
libudev msgs  not required by Netlink
  • Loading branch information
TwolDE committed Dec 2, 2024
1 parent 1c59b41 commit fa1c999
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions lib/python/Components/Netlink.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,28 @@ def __init__(self):

def parse(self):
data = self.recv(512)
if isinstance(data, bytes):
data = data.decode()
data = [x for x in data.split('\x00') if x] + [""] # avoid empty strings in the output except the final one
event = {}
for item in data:
if not item:
# terminator
yield event
event = {}
else:
try:
k, v = item.split('=', 1)
event[k] = v
except:
event[None] = item
if isinstance(data, bytes) and not data.startswith(b"libudev"):
data = data.decode("utf-8", "ignore")
data = [x for x in data.split("\x00") if x] + [""] # avoid empty strings in the output except the final one
event = {}
for item in data:
if not item:
# terminator
print(f"[netlink][parse] yield event:{event}, reset")
yield event
event = {}
else:
try:
k, v = item.split("=", 1)
event[k] = v
except:
event[None] = item


# Quick unit test (you can run this on any Linux machine)
if __name__ == '__main__':
nls = NetlinkSocket()
# print("socket no:", nls.fileno())
print("socket no:", nls.fileno())
while 1:
for item in nls.parse():
print(repr(item))

0 comments on commit fa1c999

Please sign in to comment.