Tubes¶
See the UART and serial guide for an introduction.
tube
¶
The Tube base class: buffered, timeout-aware byte streams.
Subclasses implement three primitives:
_recv_raw(numb, timeout)-> bytes. Returnb""on timeout, raiseEOFErrorwhen the other side is gone._send_raw(data)_close()
Everything else (recvuntil, sendlineafter, interactive ...) is built on top.
Like pwntools, receive helpers return b"" on timeout and keep any partial data in the
buffer, so nothing is lost. Pass strict=True (or use Tube.expect) to raise
HwnTimeoutError instead.
Tube
¶
Bases: ABC
Abstract bidirectional byte stream with pwntools-like helpers.
timeout
property
writable
¶
Default timeout for this tube (falls back to context.timeout).
recv
¶
Return up to numb bytes; whatever is available first. b"" on timeout.
recvn
¶
Return exactly numb bytes, or b"" on timeout (buffer kept).
recvuntil
¶
recvuntil(delims: Delims, drop: bool = False, timeout: object = _DEFAULT, strict: bool = False) -> bytes
Receive until one of delims is seen.
Returns everything up to and including the delimiter (excluded if drop). On timeout
returns b"" (or raises with strict=True) and leaves the data buffered.
recvregex
¶
recvregex(regex: bytes | str | Pattern[bytes], timeout: object = _DEFAULT, strict: bool = False) -> Match[bytes] | None
Receive until regex matches the buffer; consume through the match end.
Returns the match object (positions are relative to the consumed data) or None.
recvline
¶
Receive one line terminated by \n.
recvlines
¶
Receive numlines lines (fewer if the timeout hits).
recvline_contains
¶
Receive lines until one contains any of items; returns that line or b"".
recvall
¶
Receive until EOF (or until timeout elapses).
clean
¶
Drain everything that arrives until the line is quiet for timeout seconds.
can_recv
¶
Return True if data is buffered or arrives within timeout.
send
¶
Send data (str is encoded as latin-1, int as its decimal digits).
sendline
¶
Send data followed by newline.
sendafter
¶
Wait for delim (raising on timeout), then send data. Returns what was received.
sendlineafter
¶
Wait for delim (raising on timeout), then send data plus newline.
expect
¶
Like recvuntil(..., strict=True): raise if delims does not show up.
interactive
¶
interactive(prompt: str = '', stdin: IO[bytes] | None = None, stdout: IO[bytes] | None = None, escape: bytes = b'\x1d') -> None
Connect the tube to the terminal until EOF or the escape key (Ctrl-]).
Received bytes are written raw to stdout; local lines are sent as typed (the terminal stays in cooked mode, so line editing works as usual).
close
¶
Close the tube. Safe to call more than once; tubes also work as context managers.
serialtube
¶
Serial-port tube built on pyserial (pip install hwntools[serial]).
serialtube
¶
serialtube(port: str, baudrate: int | None = None, bytesize: int | None = None, parity: str | None = None, stopbits: float | None = None, xonxoff: bool | None = None, rtscts: bool | None = None, timeout: float | None = None, newline: bytes | None = None, convert_newlines: bool = False, quiet: bool = False, **kwargs: Any)
Bases: Tube
A tube over a serial port.
port may be a device path (/dev/ttyUSB0, COM3) or any pyserial URL
(rfc2217://host:port, socket://host:port, loop://). Line settings default to
context.serial.
Example
serial
¶
serial(port: str, baud: int | None = None, **kwargs: Any) -> serialtube
Open port at baud (default context.serial.baudrate) and return a tube.
remote
¶
TCP client tube (ser2net, telnet consoles, network-exposed UART bridges).
remote
¶
remote(host: str, port: int, timeout: float | None = None, connect_timeout: float = 10.0, newline: bytes | None = None, strip_telnet: bool = False, quiet: bool = False)
Bases: Tube
Connect to host:port over TCP.
Telnet option negotiation is not interpreted; raw ser2net ports work as-is. Set
strip_telnet=True to drop basic IAC sequences from received data.
process
¶
Local process tube (picocom/minicom wrappers, qemu-system with -serial stdio, ...).
process
¶
process(argv: str | Sequence[str], cwd: str | PathLike[str] | None = None, env: Mapping[str, str] | None = None, stderr_to_stdout: bool = True, timeout: float | None = None, newline: bytes | None = None, shell: bool = False, quiet: bool = False)