百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
N

ngrok-python

> 编程语言
开源

只需一行代码即可将 ngrok 安全访问接入您的 Python 应用中。

181 stars0 点赞3 次浏览
访问官网GitHub

工具介绍

只需一行代码即可将 ngrok 安全访问接入您的 Python 应用中。

Python SDK for ngrok

PyPI Supported Versions MIT licensed Apache-2.0 licensed Continuous integration

ngrok-python is the official Python SDK for ngrok that requires no binaries. Quickly enable secure production-ready connectivity to your applications and services directly from your code.

ngrok is a globally distributed gateway that provides secure connectivity for applications and services running in any environment.

Installation

The ngrok-python SDK can be installed from PyPI via pip:

bash
pip install ngrok

Quickstart

  1. Install ngrok-python

  2. Export your authtoken from the ngrok dashboard as NGROK_AUTHTOKEN in your terminal

  3. Add the following code to your application to establish connectivity via the forward method through port 9000 on localhost:

    python
    # import ngrok python sdk
    import ngrok
    import time
    
    # Establish connectivity
    listener = ngrok.forward(9000, authtoken_from_env=True)
    
    # Output ngrok url to console
    print(f"Ingress established at {listener.url()}")
    
    # Keep the listener alive
    try:
    while True:
        time.sleep(1)
    except KeyboardInterrupt:
        print("Closing listener")

That's it! Your application should now be available through the url output in your terminal.

Note You can find more examples in the examples directory.

Documentation

A full quickstart guide and API reference can be found in the ngrok-python documentation.

Authentication

To use most of ngrok's features, you'll need an authtoken. To obtain one, sign up for free at ngrok.com and retrieve it from the authtoken page in your ngrok dashboard. Once you have copied your authtoken, you can reference it in several ways.

You can set it in the NGROK_AUTHTOKEN environment variable and pass authtoken_from_env=True to the forward method:

python
ngrok.forward(authtoken_from_env=True, ...)

Or pass the authtoken directly to the forward method:

python
ngrok.forward(authtoken=token, ...)

Or set it for all connections with the set_auth_token method:

python
ngrok.set_auth_token(token)

Connection

The forward method is the easiest way to start an ngrok session and establish a listener to a specified address. If an asynchronous runtime is running, the forward method returns a promise that resolves to the public listener object.

With no arguments, the forward method will start an HTTP listener to localhost port 80:

python
listener = ngrok.forward()

You can pass the port number to forward on localhost:

python
listener = ngrok.forward(4242)

Or you can specify the host and port via a string:

python
listener = ngrok.forward("localhost:4242")

More options can be passed to the forward method to customize the connection:

python
listener = ngrok.forward(8080, basic_auth="ngrok:online1line"})
listener = ngrok.forward(8080, oauth_provider="google", oauth_allow_domains="example.com")

The second (optional) argument is the listener type, which defaults to http. To create a TCP listener:

python
listener = ngrok.forward(25565, "tcp")

Since the options are kwargs, you can also use the ** operator to pass a dictionary for configuration:

python
options = {"authtoken_from_env":True, "response_header_add":"X-Awesome:yes"}
listener = ngrok.forward(8080, **options)

See Full Configuration for the list of possible configuration options.

Disconnection

To close a listener use the disconnect method with the url of the listener to close. If there is an asynchronous runtime running the disconnect method returns a promise that resolves when the call is complete.

python
ngrok.disconnect(url)

Or omit the url to close all listeners:

python
ngrok.disconnect()

The close method on a listener will shut it down, and also stop the ngrok session if it is no longer needed. This method returns a promise that resolves when the listener is closed.

python
await listener.close()

List all Listeners

To list all current non-closed listeners use the get_listeners method. If there is an asynchronous runtime running the get_listeners method returns a promise that resolves to the list of listener objects.

python
listeners = ngrok.get_listeners()

TLS Backends

As of version 0.10.0 there is backend TLS connection support, validated by a filepath specified in the SSL_CERT_FILE environment variable, or falling back to the host OS installed trusted certificate authorities. So it is now possible to do this to connect:

python
ngrok.forward("https://127.0.0.1:3000", authtoken_from_env=True)

If the service is using certs not trusted by the OS, such as self-signed certificates, add an environment variable like this before running: SSL_CERT_FILE=/path/to/ca.crt. There is also a verify_upstream_tls=False option to disable certification verification.

Unix Sockets

You may also choose to use Unix Sockets instead of TCP. You can view an example of this here.

A socket address may be passed directly into the listener forward() call as well by prefixing the address with unix:, for example unix:/tmp/socket-123.

Builders

For more control over Sessions and Listeners, the builder classes can be used.

A minimal example using the builder class looks like the following:

python
async def create_listener():
    session = await ngrok.NgrokSessionBuilder().authtoken_from_env().connect()
    listener = await session.http_endpoint().listen()
    print (f"Ingress established at {listener.url()}")
    listener.forward("localhost:9000")

See here for a Full Configuration Example

Full Configuration

This example shows all the possible configuration items of ngrok.forward:

…

ASGI Runner

ngrok-python comes bundled with an ASGI (Asynchronous Server Gateway Interface) runner ngrok-asgi that can be used for Uvicorn, Gunicorn, Django and more, with no code.

To use prefix your start up command for a Uvicorn or Gunicorn web server with either ngrok-asgi or python -m ngrok.

Any TCP or Unix Domain Socket arguments will be used to establish connectivity automatically. The ngrok listener can be configured using command flags, for instance adding --basic-auth ngrok online1line will introduce basic authentication to the ingress listener.

Uvicorn

bash
# Basic Usage
ngrok-asgi uvicorn mysite.asgi:application

# With custom host and port
ngrok-asgi uvicorn mysite.asgi:application \
    --host localhost \
    --port 1234

# Using basic auth
ngrok-asgi uvicorn mysite.asgi:application \
    --host localhost \
    --port 1234 \
    --basic-auth ngrok online1line

# Using custom sock file
ngrok-asgi uvicorn mysite.asgi:application \
    --uds /tmp/uvicorn.sock

# Using module name
python -m ngrok uvicorn mysite.asgi:application \
    --oauth-provider google \
    --allow-emails [email protected]

Gunicorn

…

Examples

Listeners

  • HTTP
    • Full Configuration Example
  • Labeled
  • TCP
  • TLS

Frameworks

  • AIOHTTP
  • AWS APP Runner
    • with changes for Python
  • Django
    • Single File Example
    • Modify manage.py Example
    • Modify asgi.py Example
    • or via ngrok-asgi
  • Flask
  • Gunicorn
  • Hypercorn
  • Streamlit
  • Tornado
  • Uvicorn

Machine Learning

  • Gradio
    • ngrok-asgi Example
    • gradio CLI Example
  • OpenPlayground
  • GPT4ALL
  • Stable Diffusion WebUI by AUTOMATIC1111
    • ngrok-python is now built-in, see the --ngrok and --ngrok-options arguments.
  • Text Generation WebUI by oobabooga
    • ngrok-python is now built-in, see the --extension ngrok argument.

Platform Support

Pre-built binaries are provided on PyPI for the following platforms:

OS i686 x64 aarch64 arm
Windows ✓ ✓ ✓
MacOS ✓ ✓
Linux ✓ ✓ ✓
Linux musl ✓ ✓
FreeBSD *

Note ngrok-python, and [ngrok-rust](https://github.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Rustnetworkingngrokpythonpython3

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言