Skip to content

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. Return b"" on timeout, raise EOFError when 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

Tube(timeout: float | None = None, newline: bytes | None = None)

Bases: ABC

Abstract bidirectional byte stream with pwntools-like helpers.

timeout property writable

timeout: float | None

Default timeout for this tube (falls back to context.timeout).

unrecv

unrecv(data: bytes) -> None

Push data back to the front of the receive buffer.

recv

recv(numb: int = 4096, timeout: object = _DEFAULT) -> bytes

Return up to numb bytes; whatever is available first. b"" on timeout.

recvn

recvn(numb: int, timeout: object = _DEFAULT, strict: bool = False) -> bytes

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

recvline(keepends: bool = True, timeout: object = _DEFAULT, strict: bool = False) -> bytes

Receive one line terminated by \n.

recvlines

recvlines(numlines: int, keepends: bool = False, timeout: object = _DEFAULT) -> list[bytes]

Receive numlines lines (fewer if the timeout hits).

recvline_contains

recvline_contains(items: bytes | str | Iterable[bytes | str], timeout: object = _DEFAULT) -> bytes

Receive lines until one contains any of items; returns that line or b"".

recvall

recvall(timeout: object = _DEFAULT) -> bytes

Receive until EOF (or until timeout elapses).

clean

clean(timeout: float = 0.05) -> bytes

Drain everything that arrives until the line is quiet for timeout seconds.

can_recv

can_recv(timeout: float = 0.0) -> bool

Return True if data is buffered or arrives within timeout.

send

send(data: bytes | bytearray | str | int) -> None

Send data (str is encoded as latin-1, int as its decimal digits).

sendline

sendline(data: bytes | bytearray | str | int = b'') -> None

Send data followed by newline.

sendlines

sendlines(lines: Iterable[bytes | str]) -> None

Send each item of lines with sendline.

sendafter

sendafter(delim: Delims, data: bytes | str | int, timeout: object = _DEFAULT) -> bytes

Wait for delim (raising on timeout), then send data. Returns what was received.

sendlineafter

sendlineafter(delim: Delims, data: bytes | str | int, timeout: object = _DEFAULT) -> bytes

Wait for delim (raising on timeout), then send data plus newline.

expect

expect(delims: Delims, timeout: object = _DEFAULT) -> bytes

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() -> None

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

io = serial("/dev/ttyUSB0", 115200)
io.sendline(b"")
print(io.recvline())

set_baudrate

set_baudrate(baudrate: int) -> None

Change the baud rate of the open port.

send_break

send_break(duration: float = 0.25) -> None

Send a BREAK condition (some consoles use it as a magic SysRq / interrupt).

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.

from_socket classmethod

from_socket(sock: socket, timeout: float | None = None) -> remote

Wrap an already connected socket.

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)

Bases: Tube

Spawn argv with pipes for stdin/stdout (stderr merged into stdout by default).

Reading uses a background thread, so it works the same on POSIX and Windows.

shutdown_stdin

shutdown_stdin() -> None

Close the child's stdin (sends EOF).

poll

poll() -> int | None

Return the exit code, or None if still running.

wait

wait(timeout: float | None = None) -> int

Wait for the process to exit and return its exit code.

kill

kill() -> None

Kill the process immediately.