[WIP] Early code for handling BIRD's control socket.
parent
822fc1c507
commit
5cc5685ae3
@ -0,0 +1,39 @@
|
||||
"""Simple utility to interface with BIRD using the control socket."""
|
||||
|
||||
from dataclasses import dataclass
|
||||
import socket as sk
|
||||
|
||||
@dataclass
|
||||
class BirdResponse:
|
||||
text: str = ''
|
||||
codes: list[tuple[tuple[int, int], int]] = [] # List of line ranges (inclusive) and the respective code.
|
||||
|
||||
class BirdSocketConnection:
|
||||
def __init__(self, sockpath='/run/bird/bird.ctl'):
|
||||
self.socket = sk.socket(sk.AF_UNIX, sk.SOCK_STREAM)
|
||||
self.socket.setblocking(False)
|
||||
self.socket.connect(sockpath)
|
||||
|
||||
def request(self, req: str) -> BirdResponse:
|
||||
if not req.endswith('\n'):
|
||||
req = req + '\n'
|
||||
binreq = req,encode()
|
||||
self.socket.send(binreq)
|
||||
return self._parse_response()
|
||||
|
||||
def _parse_response(self):
|
||||
# We even read from the socket here in order to have the whole protocol
|
||||
# described at one place. Therefore other parts of the code do not need
|
||||
# to know anything about the protocol (maybe apart from the meaning of
|
||||
# status codes, if they are interested…)
|
||||
MAXLINELENGTH = 1000 # Pretty please…
|
||||
READSIZE = MAXLINELENGTH * 2 # Ensures that after the read at least one complete line has been read.
|
||||
lines = []
|
||||
data = b''
|
||||
# There is no way to read line-wise, so we just read everything and split afterwards.
|
||||
# This is probably very horrible.
|
||||
try:
|
||||
while True:
|
||||
data += self.socket.
|
||||
|
||||
|
Loading…
Reference in New Issue