DEV Community

Alex Laird
Alex Laird

Posted on • Edited on

2 2

pyngrok — a Python wrapper for ngrok

pyngrok - a Python wrapper for ngrok

pyngrok is a Python wrapper for ngrok that manages its own binary and puts it on your path, making ngrok readily available from anywhere on the command line and via a convenient Python API.

ngrok is a reverse proxy tool that opens secure tunnels from public URLs to localhost, perfect for exposing local web servers, building webhook integrations, enabling SSH access, testing chatbots, demoing from your own machine, and more, and its made even more powerful with native Python integration through pyngrok.

Installation

pyngrok is available on PyPI and can be installed using pip:

pip install pyngrok
Enter fullscreen mode Exit fullscreen mode

or conda:

conda install -c conda-forge pyngrok
Enter fullscreen mode Exit fullscreen mode

That's it! pyngrok is now available as a package to our Python projects, and ngrok is now available from the command line.

Basic Usage

To open a tunnel, use the connect method, which returns a NgrokTunnel that has a reference to the public URL generated by ngrok.

from pyngrok import ngrok

# Open a HTTP tunnel on the default port 80
# <NgrokTunnel: "http://<public_sub>.ngrok.io" -> "http://localhost:80">
http_tunnel = ngrok.connect()

# Open a SSH tunnel
# <NgrokTunnel: "tcp://0.tcp.ngrok.io:12345" -> "localhost:22">
ssh_tunnel = ngrok.connect(22, "tcp")

# Open a named tunnel from the config file
named_tunnel = ngrok.connect(name="my_tunnel_name")
Enter fullscreen mode Exit fullscreen mode

The connect method takes kwargs as well, which allows us to pass additional tunnel configurations that are supported by ngrok, as documented here.

ngrok's Edges

To use ngrok's Edges with pyngrok, first configure an Edge on ngrok's dashboard (with at least one Endpoint mapped to the Edge), and define a labeled tunnel in the ngrok config file that points to the Edge.

tunnels:
  some-edge-tunnel:
    labels:
      - edge=my_edge_id
    addr: http://localhost:80
Enter fullscreen mode Exit fullscreen mode

To start a labeled tunnel in pyngrok, pass its name to connect.

from pyngrok import ngrok

# Open the Edge tunnel that is defind in the config file
named_tunnel = ngrok.connect(name="some-edge-tunnel")
Enter fullscreen mode Exit fullscreen mode

Once an Edge tunnel is started, it can be managed through ngrok's dashboard.

Command Line Usage

This package puts the default ngrok binary on our path, so all features of ngrok are available on the command line.

ngrok http 80
Enter fullscreen mode Exit fullscreen mode

For details on how to fully leverage ngrok from the command line, see ngrok's official documentation.

Documentation

For more advanced usage, pyngrok's official documentation is available at http://pyngrok.readthedocs.io.

ngrok Version Compatibility

pyngrok is compatible with ngrok v2 and v3, but by default it will install v3. To install v2 instead, set ngrok_version in PyngrokConfig:

from pyngrok import conf

conf.get_default().ngrok_version = "v2"
Enter fullscreen mode Exit fullscreen mode

Python 2.7

The last version of pyngrok that supports Python 2.7 is 4.1.x, so we need to pin pyngrok>=4.1,<4.2 if we still want to use pyngrok with this version of Python.

Contributing

If you would like to get involved, be sure to review the Contribution Guide.

Want to contribute financially? sponsorship would also be greatly appreciated!

Billboard image

The fastest way to detect downtimes

Join Vercel, CrowdStrike, and thousands of other teams that trust Checkly to streamline monitoring.

Get started now

Top comments (0)

Image of Bright Data

Maintain Seamless Data Collection – No more rotating IPs or server bans.

Avoid detection with our dynamic IP solutions. Perfect for continuous data scraping without interruptions.

Avoid Detection

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay