python/console_socket: accept existing FD in initializer
Useful if we want to use ConsoleSocket() for a socket created by socketpair(). Signed-off-by: John Snow <jsnow@redhat.com> Reviewed-by: Ani Sinha <anisinha@redhat.com> Reviewed-by: Daniel P. Berrangé <berrange@redhat.com> Message-id: 20230928044943.849073-4-jsnow@redhat.com Signed-off-by: John Snow <jsnow@redhat.com>
This commit is contained in:
parent
612b3ba218
commit
5f263cb1cc
@ -24,19 +24,32 @@ class ConsoleSocket(socket.socket):
|
|||||||
"""
|
"""
|
||||||
ConsoleSocket represents a socket attached to a char device.
|
ConsoleSocket represents a socket attached to a char device.
|
||||||
|
|
||||||
Optionally (if drain==True), drains the socket and places the bytes
|
:param address: An AF_UNIX path or address.
|
||||||
into an in memory buffer for later processing.
|
:param sock_fd: Optionally, an existing socket file descriptor.
|
||||||
|
One of address or sock_fd must be specified.
|
||||||
Optionally a file path can be passed in and we will also
|
:param file: Optionally, a filename to log to.
|
||||||
dump the characters to this file for debugging purposes.
|
:param drain: Optionally, drains the socket and places the bytes
|
||||||
|
into an in memory buffer for later processing.
|
||||||
"""
|
"""
|
||||||
def __init__(self, address: str, file: Optional[str] = None,
|
def __init__(self,
|
||||||
|
address: Optional[str] = None,
|
||||||
|
sock_fd: Optional[int] = None,
|
||||||
|
file: Optional[str] = None,
|
||||||
drain: bool = False):
|
drain: bool = False):
|
||||||
|
if address is None and sock_fd is None:
|
||||||
|
raise ValueError("one of 'address' or 'sock_fd' must be specified")
|
||||||
|
if address is not None and sock_fd is not None:
|
||||||
|
raise ValueError("can't specify both 'address' and 'sock_fd'")
|
||||||
|
|
||||||
self._recv_timeout_sec = 300.0
|
self._recv_timeout_sec = 300.0
|
||||||
self._sleep_time = 0.5
|
self._sleep_time = 0.5
|
||||||
self._buffer: Deque[int] = deque()
|
self._buffer: Deque[int] = deque()
|
||||||
socket.socket.__init__(self, socket.AF_UNIX, socket.SOCK_STREAM)
|
if address is not None:
|
||||||
self.connect(address)
|
socket.socket.__init__(self, socket.AF_UNIX, socket.SOCK_STREAM)
|
||||||
|
self.connect(address)
|
||||||
|
else:
|
||||||
|
assert sock_fd is not None
|
||||||
|
socket.socket.__init__(self, fileno=sock_fd)
|
||||||
self._logfile = None
|
self._logfile = None
|
||||||
if file:
|
if file:
|
||||||
# pylint: disable=consider-using-with
|
# pylint: disable=consider-using-with
|
||||||
|
Loading…
x
Reference in New Issue
Block a user