---
title: PortForward
description: A running port-forward. Use as a context manager, or call stop().
---

```python
@final
class PortForward
```

A running port-forward. Use as a context manager, or call `stop()`.

Keep a reference: dropping this object stops the forward and cuts its open connections.

## Constructor

`PortForward` declares no `__new__` or `__init__`, so it is not constructed directly: an instance comes back from another call in this package.

## Properties

### `local_address`

```python
local_address: str
```

The local address to connect to, `host:port`, with the port the OS picked.

### `running`

```python
running: bool
```

Whether the forward is still serving.

## Methods

### `stop`

```python
def stop(timeout: SupportsFloat | SupportsIndex | None = None) -> PortForwardReport
```

Stops accepting, waits for the connections still open to end, and returns the report.

With `timeout`, connections still open after that many seconds are cut and listed as
`"failed"`. Without it, a client that keeps its connection open keeps this waiting.
Callable again, with the same report.

### `stop_async`

```python
def stop_async(
    timeout: SupportsFloat | SupportsIndex | None = None,
) -> PortForwardReport
```

The awaitable twin of `stop`.

## Special methods

### `__aenter__`

```python
def __aenter__() -> PortForward
```

`async with session.port_forward(...) as t:`.

### `__aexit__`

```python
def __aexit__(
    exc_type: Any | None = None,
    exc_value: Any | None = None,
    traceback: Any | None = None,
) -> bool
```

`__exit__`, awaited.

### `__enter__`

```python
def __enter__() -> PortForward
```

### `__exit__`

```python
def __exit__(
    exc_type: Any | None = None,
    exc_value: Any | None = None,
    traceback: Any | None = None,
) -> bool
```

Stops the forward, waiting for its open connections. An error stopping it doesn't mask
an exception already in flight.

### `__repr__`

```python
def __repr__() -> str
```

## Provenance

This page is generated from `bindings/microvms-py/microvms.pyi` by Griffe 2.3.0, run from `site/scripts/gen-reference.mjs` on every `pnpm run sync`, so an edit made here is overwritten by the next run.

To change the page, change the doc comment in the binding's Rust source, then regenerate the declarations with `mise run stubs`. `mise run stubs:check` fails when the committed file no longer matches the source.