Skip to main content

Python port of the localtunnel.me Client | Expose localhost to the world!

Project description

LocalTunnel Python Client

PyPI version License Python Versions

Python port of the Localtunnel Client

Expose your localhost to the 🌎 for easy testing and sharing:

  • 👷🏻‍♂️ Actively maintained

A Python client for LocalTunnel, enabling developers to expose their local servers to the internet with minimal setup. Designed for ease of use, flexibility, and lightweight integration, this library supports both CLI and programmatic APIs.


Overview

LocalTunnel provides a simple way to make a local development server accessible to anyone on the internet. Unlike similar tools like Ngrok, this library is lightweight, open-source, and Python-native, making it perfect for developers who prefer seamless integration into their Python projects.


Installation

Install using a package manager 📦:

via pip

pip install localtunnel

via uv

uv add localtunnel

via poetry

poetry add Localtunnel

For contributors or advanced users, clone the repository and install in editable mode:

git clone https://github.com/gweidart/localtunnel.git
cd localtunnel
pip install -e .

Features

  • Expose Local Servers Effortlessly:

    • Quickly share your local development server with a public URL.
    • Perfect for testing webhooks, sharing progress with team members, or debugging remotely.
  • Custom Subdomains:

    • Use a custom subdomain to make your server URL more predictable and user-friendly.
    • Example: https://my-custom-subdomain.loca.lt
  • Robust Retry Mechanisms:

    • Ensure tunnel connections are resilient with customizable retry strategies, including exponential backoff.
  • Monitoring and Lifecycle Management:

    • Built-in support for monitoring tunnels to handle unexpected disruptions.
    • Automatically recover or notify when a tunnel goes offline.
  • Flexible Header Transformations:

    • Modify request headers dynamically using HeaderTransformer.
  • Lightweight CLI Tool:

    • A simple command-line interface for quick setup and deployment.
  • Seamless Integration:

    • Import the library directly into your Python project and manage tunnels programmatically.
  • Extensive Logging:

    • Fully customizable logging via Loguru.

Quick Start

Programmatic API

Here’s how to expose a local server programmatically:

import asyncio
from localtunnel.tunnel_manager import TunnelManager

async def main():
    manager = TunnelManager()
    manager.add_tunnel(port=8000, subdomain="my-subdomain")

    try:
        await manager.open_all()
        for tunnel in manager.tunnels:
            print(f"Tunnel open at URL: {tunnel.get_tunnel_url()}")

        # Keep running
        await asyncio.Event().wait()
    finally:
        await manager.close_all()

asyncio.run(main())

CLI Usage

Expose a local server directly from the command line:

lt --port 3002 -s my-subdomain

Available arguments:

Argument Description
-p, --port Local port to expose via the tunnel (required).
-s, --subdomain Optional subdomain for the tunnel.
-t, --host LocalTunnel server URL (default: https://localtunnel.me).
-m, --monitor Enable monitoring of the tunnel.
-l, --log-level Set the log level (e.g., DEBUG, INFO, WARNING, ERROR, CRITICAL).

Advanced Usage

Create a new localtunnel

import asyncio
from localtunnel.tunnel_manager import TunnelManager

async def main():
    manager = TunnelManager()
    manager.add_tunnel(port=8000, subdomain="my-subdomain")

    try:
        await manager.open_all()
        for tunnel in manager.tunnels:
            print(f"Tunnel open at URL: {tunnel.get_tunnel_url()}")

        # Keep running
        await asyncio.Event().wait()
    finally:
        await manager.close_all()

asyncio.run(main())

Custom Header Transformations

Modify headers dynamically using HeaderTransformer:

from localtunnel.header_transformer import HeaderTransformerFactory

transformer = HeaderTransformerFactory.create_transformer(
    transformer_type="host", host="my-custom-host"
)
headers = {"Authorization": "Bearer token"}
transformed_headers = transformer.transform(headers)
print(transformed_headers)

Retry Strategies

Implement robust retry mechanisms for tunnel connections:

from localtunnel.utils import ExponentialBackoffRetryTemplate

retry_strategy = ExponentialBackoffRetryTemplate(base_delay=1.0, max_delay=10.0)
retry_strategy.retry(some_function, retries=5)

Managing Multiple Tunnels

Use TunnelManager to handle multiple tunnels seamlessly:

from localtunnel.tunnel_manager import TunnelManager

manager = TunnelManager()
manager.add_tunnel(port=8000, subdomain="app1")
manager.add_tunnel(port=8001, subdomain="app2")
await manager.open_all()

Troubleshooting

  • Issue: Tunnel connection drops frequently.

    • Solution: Enable monitoring with TunnelManager.
  • Issue: Logs are too verbose.

    • Solution: Customize log levels using --log-level in the CLI or logger.add() in code.
  • Issue: Custom subdomain not working.

    • Solution: Ensure the subdomain is available and correctly passed to add_tunnel().

Contributing

We welcome contributions! Here's how you can get involved:

  1. Fork the repository.
  2. Create a new branch for your feature or bugfix.
  3. Submit a pull request with a detailed description.

License

This project is licensed under the MIT License. See the LICENSE file for details.


Links and Acknowledgments

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

localtunnel_py-0.1.0.tar.gz (15.5 kB view details)

Uploaded Source

Built Distribution

localtunnel_py-0.1.0-py3-none-any.whl (14.8 kB view details)

Uploaded Python 3

File details

Details for the file localtunnel_py-0.1.0.tar.gz.

File metadata

  • Download URL: localtunnel_py-0.1.0.tar.gz
  • Upload date:
  • Size: 15.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for localtunnel_py-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b32c252615755ccaf9999384614d7e2ee62e034067882425a3f6598837386ea2
MD5 22910bb300f505725f74c722b756619f
BLAKE2b-256 de6de694a980be9d3332802a029771084dce043a9764ec0d7d0a838484226f69

See more details on using hashes here.

Provenance

The following attestation bundles were made for localtunnel_py-0.1.0.tar.gz:

Publisher: pypi-workflow.yaml on gweidart/localtunnel-py

Attestations:

File details

Details for the file localtunnel_py-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for localtunnel_py-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2277e705dcd7e3dade965fdce5fc57217598f8941f965625dec3b20b67cd10e5
MD5 25fbf303bef861cb1571bc7e343bf2c0
BLAKE2b-256 a89662f977513e13e853dc497c9a72cbd736a9c4a5e59abc7852ec16e4cd2c22

See more details on using hashes here.

Provenance

The following attestation bundles were made for localtunnel_py-0.1.0-py3-none-any.whl:

Publisher: pypi-workflow.yaml on gweidart/localtunnel-py

Attestations:

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page