Open a TCP tunnel to a port inside the sandbox.
Creates a local TCP listener that forwards connections through a yamux-multiplexed WebSocket to the specified port inside the sandbox. Works with any TCP protocol (databases, Redis, HTTP, etc.).
Use as a context manager for automatic cleanup::
with sandbox.tunnel(remote_port=5432) as t:
conn = psycopg2.connect(host="127.0.0.1", port=t.local_port)
Or manage the lifecycle explicitly::
t = sandbox.tunnel(remote_port=5432)
# ... use tunnel ...
t.close()
TCP port inside the sandbox to tunnel to (1-65535).
Local port to listen on. Defaults to mirroring remote_port. Use 0 to let the OS pick an available port.
Maximum number of automatic reconnect attempts when the WebSocket session drops. Set to 0 to disable.