Cloudflared
cloudflared(port=8000, metrics_port=None, tunnel_id=None, config_path=None, force=False, max_retries=10)
A context manager for running a Cloudflare tunnel. It downloads the appropriate Cloudflare binary, starts the tunnel, and yields the tunnel URL. After the context is exited, it terminates the tunnel.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
port |
int
|
The local port to tunnel. Defaults to 8000. |
8000
|
metrics_port |
int
|
The port to serve metrics on. If not provided, a random port between 8100 and 9000 is chosen. |
None
|
tunnel_id |
str
|
The ID of the tunnel to run. If not provided, a tunnel to the local port is created. |
None
|
config_path |
str
|
The path to a Cloudflare configuration file. If provided, the 'run' command is used with this configuration. |
None
|
force |
bool
|
If True, forces the download of the Cloudflare binary even if it already exists. Defaults to False. |
False
|
max_retries |
int
|
The maximum number of times to retry connecting to the Cloudflare Edge. Defaults to 10. |
10
|
Yields:
Name | Type | Description |
---|---|---|
str |
The URL of the Cloudflare tunnel. |
Raises:
Type | Description |
---|---|
Exception
|
If it can't connect to the Cloudflare Edge after max_retries attempts. |
Side Effects
Downloads the Cloudflare binary, starts a subprocess running the Cloudflare tunnel, and modifies the permissions of the Cloudflare binary.
Examples:
>>> with cloudflared(port=8080) as url:
... print(f"Cloudflare tunnel running at {url}")
Cloudflare tunnel running at https://randomsubdomain.trycloudflare.com