CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-pyngrok

A Python wrapper for ngrok that manages its own binary, making ngrok available via a convenient Python API

Pending
Overview
Eval results
Files

installation.mddocs/

Installation and Binary Management

Automatic ngrok binary download, installation, and management with support for multiple ngrok versions and platform detection.

Capabilities

Basic Installation

Automatic ngrok binary installation with version management.

def install_ngrok(pyngrok_config=None):
    """
    Download, install, and initialize ngrok for the given config.
    If ngrok and its config are already installed, does nothing.

    Parameters:
    - pyngrok_config (PyngrokConfig, optional): Configuration override
    """

Usage Examples:

from pyngrok import ngrok, conf

# Install with default configuration
ngrok.install_ngrok()

# Install with custom configuration
custom_config = conf.PyngrokConfig(
    ngrok_path="/usr/local/bin/ngrok",
    ngrok_version="v3"
)
ngrok.install_ngrok(custom_config)

# Installation is idempotent - safe to call multiple times
ngrok.install_ngrok()  # Does nothing if already installed

Version Management

Get version information and update ngrok binary.

def get_version(pyngrok_config=None):
    """
    Get tuple with ngrok and pyngrok versions.

    Parameters:
    - pyngrok_config (PyngrokConfig, optional): Configuration override

    Returns:
    tuple[str, str]: (ngrok_version, pyngrok_version)
    """

def update(pyngrok_config=None):
    """
    Update ngrok binary if an update is available.

    Parameters:
    - pyngrok_config (PyngrokConfig, optional): Configuration override

    Returns:
    str: Result from the ngrok update command
    """

Usage Examples:

from pyngrok import ngrok

# Check current versions
ngrok_version, pyngrok_version = ngrok.get_version()
print(f"ngrok version: {ngrok_version}")
print(f"pyngrok version: {pyngrok_version}")

# Update ngrok if newer version available
try:
    update_result = ngrok.update()
    print(f"Update result: {update_result}")
except Exception as e:
    print(f"Update failed: {e}")

Authentication Setup

Configure authentication tokens and API keys for ngrok features.

def set_auth_token(token, pyngrok_config=None):
    """
    Set ngrok auth token in config file for advanced features.
    Installs ngrok if not already present.

    Parameters:
    - token (str): The auth token to set
    - pyngrok_config (PyngrokConfig, optional): Configuration override
    """

def set_api_key(key, pyngrok_config=None):
    """
    Set ngrok API key in config file for advanced features.
    Installs ngrok if not already present.

    Parameters:
    - key (str): The API key to set  
    - pyngrok_config (PyngrokConfig, optional): Configuration override
    """

Usage Examples:

from pyngrok import ngrok

# Set auth token for multiple concurrent tunnels
ngrok.set_auth_token("your_ngrok_authtoken_here")

# Set API key for advanced features (v3 only)
ngrok.set_api_key("your_ngrok_api_key_here")

# Now you can create multiple tunnels
tunnel1 = ngrok.connect("8000")
tunnel2 = ngrok.connect("9000") 
tunnel3 = ngrok.connect("22", "tcp")

Low-Level Installation Functions

Direct control over ngrok installation process.

def get_default_ngrok_dir():
    """
    Get the default ngrok directory for current system.

    Returns:
    str: The default ngrok directory path
    """

def get_system():
    """
    Get the friendly name of the current operating system.

    Returns:
    str: OS name ("darwin", "windows", "linux", "freebsd")

    Raises:
    PyngrokNgrokInstallError: When platform is not supported
    """

def get_arch():
    """
    Get the architecture of the current system.

    Returns:
    str: Architecture name ("i386", "x86_64", "x86_64_arm", etc.)
    """

def install_ngrok(ngrok_path, ngrok_version="v3", **kwargs):
    """
    Download and install ngrok binary to specified path.

    Parameters:
    - ngrok_path (str): Path where ngrok binary will be installed
    - ngrok_version (str): Major version ("v2" or "v3")
    - **kwargs: Additional options passed to download function

    Raises:
    PyngrokError: When ngrok_version is not supported
    PyngrokNgrokInstallError: When installation fails
    """

Usage Examples:

from pyngrok import installer

# Get system information
print(f"Default ngrok directory: {installer.get_default_ngrok_dir()}")
print(f"System: {installer.get_system()}")
print(f"Architecture: {installer.get_arch()}")

# Install ngrok to custom location
custom_path = "/opt/ngrok/ngrok"
installer.install_ngrok(custom_path, ngrok_version="v3")

# Install specific version with custom timeout
installer.install_ngrok(
    "/usr/local/bin/ngrok", 
    ngrok_version="v2",
    timeout=30  # 30 second download timeout
)

Configuration File Management

Manage ngrok configuration files and validation.

def get_ngrok_config(config_path, use_cache=True, ngrok_version="v3", config_version="2"):
    """
    Get ngrok config from the given path.

    Parameters:
    - config_path (str): Path to ngrok config file
    - use_cache (bool): Whether to use cached config if available
    - ngrok_version (str): Major version of ngrok
    - config_version (str): ngrok config version

    Returns:
    dict: The ngrok configuration
    """

def install_default_config(config_path, data=None, ngrok_version="v3", config_version="2"):
    """
    Install default config to the specified path.

    Parameters:
    - config_path (str): Path where config should be installed
    - data (dict, optional): Additional config data to include
    - ngrok_version (str): Major version of ngrok
    - config_version (str): ngrok config version
    """

def validate_config(data):
    """
    Validate config data for compatibility with pyngrok.

    Parameters:
    - data (dict): Config data to validate

    Raises:
    PyngrokError: When config contains incompatible settings
    """

Usage Examples:

from pyngrok import installer, conf
import os

# Get default config path
config = conf.get_default()
config_path = conf.get_config_path(config)

# Read existing config
if os.path.exists(config_path):
    ngrok_config = installer.get_ngrok_config(config_path)
    print(f"Current config: {ngrok_config}")

# Install default config with custom data
custom_data = {
    "authtoken": "your_token_here",
    "region": "eu"
}
installer.install_default_config(config_path, custom_data)

# Validate config before using
test_config = {
    "web_addr": "localhost:4040",
    "log_level": "info"
}
try:
    installer.validate_config(test_config)
    print("Config is valid")
except Exception as e:
    print(f"Config validation failed: {e}")

Platform and Version Detection

Utilities for detecting platform and version compatibility.

def get_ngrok_bin():
    """
    Get ngrok executable name for current system.

    Returns:
    str: Executable name ("ngrok" on Unix, "ngrok.exe" on Windows)
    """

def get_ngrok_cdn_url(ngrok_version):
    """
    Get CDN download URL for current platform and ngrok version.

    Parameters:
    - ngrok_version (str): Major version ("v2" or "v3")

    Returns:
    str: CDN download URL for current platform

    Raises:
    PyngrokNgrokInstallError: When platform is not supported
    """

Usage Examples:

from pyngrok import installer

# Get platform-specific executable name
executable = installer.get_ngrok_bin()
print(f"ngrok executable: {executable}")

# Get download URLs for different versions
v2_url = installer.get_ngrok_cdn_url("v2")
v3_url = installer.get_ngrok_cdn_url("v3")

print(f"ngrok v2 download URL: {v2_url}")
print(f"ngrok v3 download URL: {v3_url}")

Installation Constants

Platform and version information available as module constants.

# Platform download URLs
PLATFORMS: dict          # v2 platform-specific download URLs
PLATFORMS_V3: dict       # v3 platform-specific download URLs

# Supported configurations  
UNIX_BINARIES: list      # List of Unix-like OS names
SUPPORTED_NGROK_VERSIONS: list  # List of supported ngrok versions

# Download settings
DEFAULT_DOWNLOAD_TIMEOUT: int   # Default download timeout in seconds
DEFAULT_RETRY_COUNT: int        # Default number of download retries

# Thread synchronization
config_file_lock: threading.RLock  # Lock for config file operations

Usage Examples:

from pyngrok import installer

# Check supported versions
print(f"Supported versions: {installer.SUPPORTED_NGROK_VERSIONS}")

# Check if current platform is Unix-like
system = installer.get_system()
is_unix = system in installer.UNIX_BINARIES
print(f"Unix-like system: {is_unix}")

# Access platform URLs
print(f"Available v3 platforms: {list(installer.PLATFORMS_V3.keys())}")

Error Handling and Troubleshooting

Common installation error scenarios and handling.

Usage Examples:

from pyngrok import ngrok, installer
from pyngrok.exception import PyngrokNgrokInstallError, PyngrokError

# Handle installation errors
try:
    ngrok.install_ngrok()
except PyngrokNgrokInstallError as e:
    print(f"Installation failed: {e}")
    # Try manual installation or check network connectivity
except PyngrokError as e:
    print(f"General pyngrok error: {e}")

# Verify installation
try:
    version = ngrok.get_version()
    print(f"Installation successful: {version}")
except Exception as e:
    print(f"Installation verification failed: {e}")

# Handle platform compatibility
try:
    system = installer.get_system()
    arch = installer.get_arch()
    print(f"Platform: {system}-{arch}")
except PyngrokNgrokInstallError as e:
    print(f"Unsupported platform: {e}")

# Custom installation with error handling
def safe_install(custom_path, version="v3", max_retries=3):
    """Install ngrok with retry logic"""
    for attempt in range(max_retries):
        try:
            installer.install_ngrok(custom_path, ngrok_version=version)
            print(f"Installation successful on attempt {attempt + 1}")
            return
        except Exception as e:
            print(f"Attempt {attempt + 1} failed: {e}")
            if attempt == max_retries - 1:
                print("All installation attempts failed")
                raise

# Use safe installation  
safe_install("/opt/ngrok/ngrok")

Install with Tessl CLI

npx tessl i tessl/pypi-pyngrok

docs

agent-inspection.md

api-integration.md

configuration.md

index.md

installation.md

process-management.md

tunnel-management.md

tile.json