The ngrok Agent SDK for Python
Project description
The ngrok Agent SDK for Python
Note: This is beta-quality software. Interfaces may change without warning.
ngrok is a globally distributed reverse proxy commonly used for quickly getting a public URL to a service running inside a private network, such as on your local laptop. The ngrok agent is usually deployed inside a private network and is used to communicate with the ngrok cloud service.
This is the ngrok agent in library form, suitable for integrating directly into Python applications. This allows you to quickly build ngrok into your application with no separate process to manage.
If you're looking for the previous agent downloader project, it's over here.
Documentation
A quickstart guide and a full API reference are included in the ngrok-python API documentation.
Quickstart
- Install the
ngrok-python
package from PyPI usingpip
:
python -m pip install ngrok
-
After you've installed the package, you'll need an authtoken. Retrieve one on the authtoken page of your ngrok dashboard.
-
Add the following code block using the connect method to expose your python application at port
9000
onlocalhost
:
import ngrok
tunnel = ngrok.connect(9000, authtoken_from_env=True)
print (f"Ingress established at {tunnel.url()}")
You can find more examples in the /examples directory.
Authorization
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 connect method:
ngrok.connect(authtoken_from_env=True, ...)
Or pass the authtoken directly to the connect method:
ngrok.connect(authtoken=token, ...)
Or set it for all connections with the set_auth_token method:
ngrok.set_auth_token(token)
Connection
The connect method is the easiest way to start an ngrok session and establish a tunnel to a specified address. If an asynchronous runtime is running, the connect method returns a promise that resolves to the public tunnel object.
With no arguments, the connect method will start an HTTP tunnel to localhost
port 80
:
tunnel = ngrok.connect()
You can pass the port number to forward on localhost
:
tunnel = ngrok.connect(4242)
Or you can specify the host and port via a string:
tunnel = ngrok.connect("localhost:4242")
More options can be passed to the connect
method to customize the connection:
tunnel = ngrok.connect(8080, basic_auth="ngrok:online1line"})
tunnel = ngrok.connect(8080, oauth_provider="google", oauth_allow_domains="example.com")
The second (optional) argument is the tunnel type, which defaults to http
. To create a TCP tunnel:
tunnel = ngrok.connect(25565, "tcp")
Since the options are kwargs, you can also use the **
operator to pass a dictionary for configuration:
options = {"authtoken_from_env":True, "response_header_add":"X-Awesome:yes"}
tunnel = ngrok.connect(8080, **options)
Disconnection
To close a tunnel use the disconnect method with the url
of the tunnel to close. If there is an asynchronous runtime running the disconnect method returns a promise that resolves when the call is complete.
ngrok.disconnect(url)
Or omit the url
to close all tunnels:
ngrok.disconnect()
The close method on a tunnel 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 tunnel is closed.
await tunnel.close()
Listing Tunnels
To list all current non-closed tunnels use the get_tunnels method. If there is an asynchronous runtime running the get_tunnels method returns a promise that resolves to the list of tunnel objects.
tunnels = ngrok.get_tunnels()
Full Configuration
This example shows all the possible configuration items of ngrok.connect:
tunnel = ngrok.connect(
# session configuration
addr="localhost:8080",
authtoken="<authtoken>",
authtoken_from_env=True,
session_metadata="Online in One Line",
# tunnel configuration
basic_auth=["ngrok:online1line"],
circuit_breaker=0.1,
compression=True,
domain="<domain>",
ip_restriction_allow_cidrs="0.0.0.0/0",
ip_restriction_deny_cidrs="10.1.1.1/32",
metadata="example tunnel metadata from python",
mutual_tls_cas=load_file("ca.crt"),
oauth_provider="google",
oauth_allow_domains=["<domain>"],
oauth_allow_emails=["<email>"],
oauth_scopes=["<scope>"],
oidc_issuer_url="<url>",
oidc_client_id="<id>",
oidc_client_secret="<secret>",
oidc_allow_domains=["<domain>"],
oidc_allow_emails=["<email>"],
oidc_scopes=["<scope>"],
proxy_proto="", # One of: "", "1", "2"
request_header_remove="X-Req-Nope",
response_header_remove="X-Res-Nope",
request_header_add="X-Req-Yup:true",
response_header_add="X-Res-Yup:true",
schemes=["HTTPS"],
verify_webhook_provider="twilio",
verify_webhook_secret="asdf",
websocket_tcp_converter=True,
)
ASGI Runner - Tunnels to Uvicorn, Gunicorn, Django and More, With No Code
Prefix the command line which starts up 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. There are many command line arguments to configure the Tunnel used, for instance adding --basic-auth ngrok online1line
will introduce basic authentication to the ingress tunnel.
Uvicorn
Examples:
ngrok-asgi uvicorn mysite.asgi:application
ngrok-asgi uvicorn mysite.asgi:application --host localhost --port 1234
ngrok-asgi uvicorn mysite.asgi:application --host localhost --port 1234 --basic-auth ngrok online1line
ngrok-asgi uvicorn mysite.asgi:application --uds /tmp/uvicorn.sock
# Can use the module name as well, such as:
python -m ngrok uvicorn mysite.asgi:application --oauth-provider google --allow-emails bob@example.com
Gunicorn
Examples:
ngrok-asgi gunicorn mysite.asgi:application -k uvicorn.workers.UvicornWorker
ngrok-asgi gunicorn mysite.asgi:application -k uvicorn.workers.UvicornWorker --webhook-verification twilio s3cr3t
ngrok-asgi gunicorn mysite.asgi:application -k uvicorn.workers.UvicornWorker --bind localhost:1234
ngrok-asgi gunicorn mysite.asgi:application -k uvicorn.workers.UvicornWorker --bind unix:/tmp/gunicorn.sock
# Can use the module name as well, such as:
python -m ngrok gunicorn mysite.asgi:application -k uvicorn.workers.UvicornWorker --response-header X-Awesome True
Examples
Frameworks
- Aiohttp - Example
- AWS App Runner - See the ngrok SDK Serverless Example repository, making the necessary changes to use Python instead of NodeJS
- Django - Single File Example, Modify manage.py Example, Modify asgi.py Example, or use the
ngrok-asgi
ASGI Runner discussed above - Flask - Example
- Gunicorn - Use the
ngrok-asgi
ASGI Runner discussed above - Streamlit - Example
- Tornado - Example
- Uvicorn - Example, or use the
ngrok-asgi
ASGI Runner discussed above
Machine Learning
- Gradio - ngrok-asgi Example, gradio CLI Example sharing machine learning apps
- OpenPlayground - Example of an LLM playground you can run on your laptop
- GPT4ALL - Example of running the GPT4All-L Snoozy 13B model with a Gradio frontend
Tunnel Types
- HTTP - Minimal Example, Full Configuration Example
- Labeled - Example
- TCP - Example
- TLS - Example
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 tunnel forward()
call as well by prefixing the address with unix:
, for example unix:/tmp/socket-123
.
Builders
For more control over Sessions and Tunnels, the builder classes can be used.
A minimal example using the builder class looks like the following:
async def create_tunnel():
session = await ngrok.NgrokSessionBuilder().authtoken_from_env().connect()
tunnel = await session.http_endpoint().listen()
print (f"Ingress established at {tunnel.url()}")
tunnel.forward("localhost:9000")
See here for a Full Configuration Example
Platform Support
Pre-built binaries are provided on PyPI for the following platforms:
OS | i686 | x64 | aarch64 | arm |
---|---|---|---|---|
Windows | ✓ | ✓ | * | |
MacOS | ✓ | ✓ | ||
Linux | ✓ | ✓ | ✓ | |
Linux musl | ✓ | ✓ | ||
FreeBSD | * |
ngrok-python, and ngrok-rust which it depends on, are open source, so it may be possible to build them for other platforms.
- Windows-aarch64 will be supported after the next release of Ring.
- FreeBSD-x64 is built by the release process, but PyPI won't accept BSD flavors.
Dependencies
This project relies on PyO3, an excellent system to ease development and building of Rust plugins for Python.
Thank you to OpenIoTHub for handing over the ngrok name on PyPI.
Change Log
Changes are tracked in CHANGELOG.md.
License
This project is licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in ngrok-python by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Hashes for ngrok-0.10.1-cp37-abi3-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d4462227e6754a92caee56a42bcb6ab0c92369bf453e32b84532d16f69d6ec14 |
|
MD5 | 65870e9a60780b7c1401d46de37669d1 |
|
BLAKE2b-256 | 78d5407e6f6f57bef6c73780194453447977212780b9edbc31d04a7a63ed07ee |
Hashes for ngrok-0.10.1-cp37-abi3-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d4fc0682e10f13f554a506427bdd8cbcbabb5d2086264fe6c82e56e2a7018e0f |
|
MD5 | 7b7201c58710af369cf7232932333995 |
|
BLAKE2b-256 | 31f9c3ca76cb6e7d51ab1d86f65564e70e30fca6c3d9b8f6a3349dd342608768 |
Hashes for ngrok-0.10.1-cp37-abi3-musllinux_1_2_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2de024fa26371d67f853f7304b0b62729bcdba8282576b616e4f78e53dfa54cf |
|
MD5 | 0e2c80c66b2194fdc2fd22ae606e57a3 |
|
BLAKE2b-256 | 0d8dd156ebb024284acf5a24b5cbd2b6f2cd118e0ca6d3ebebb37cfd396d4295 |
Hashes for ngrok-0.10.1-cp37-abi3-musllinux_1_2_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8689f89a338dd3393859873a92d1b61d033278f045bedb410037bac90f6a786a |
|
MD5 | 67f0153fafcc456a114118c5577821ce |
|
BLAKE2b-256 | f490e4a49f3bc623174c703f5ccf94d13092d692f389b076e1bc13ca94f5e414 |
Hashes for ngrok-0.10.1-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 497b31a3d99110d3a963720d5aa68096c9e8b75c47f03b2a216320332f32094b |
|
MD5 | fd421c2d2a70f4eeb9d392f07590208c |
|
BLAKE2b-256 | 82fa7db9ddc6d0f816f7df6bdae5b3bbe3aba8821bfda66ec899884e724f193a |
Hashes for ngrok-0.10.1-cp37-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6982a174d1635d8cf5109f456f4bfd5ce5088406673af57e48c90a30baa3b2b3 |
|
MD5 | ff92f6700b80bc081b94fa4cd7fc2f1e |
|
BLAKE2b-256 | 61c65ccbb0c50f0f26695f6116675a0f79431413dc93bad8a0b0545f4bea8189 |
Hashes for ngrok-0.10.1-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fa89722aebc3a91e7809a83b28410ed593d7639042ab2efdf5fccc1e36776d21 |
|
MD5 | ef2f748b675854f5cfbab600fd2eb2c8 |
|
BLAKE2b-256 | 537937048fc55a0503458fecb376d2f34ac50ac229f6e86c00bf293eb53b0fb9 |
Hashes for ngrok-0.10.1-cp37-abi3-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e328f1e9ce8979db6e26d108a4f7f5f1f9c295c9be690f65b474e3c1f536a2e1 |
|
MD5 | 36e59d001cf6c9c9ae83726cc77d1728 |
|
BLAKE2b-256 | 4d7d4670cd1e7014bf8f7c6a41edccc3021cdc9abce58b34ad02ab30e92925c2 |
Hashes for ngrok-0.10.1-cp37-abi3-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ce8b2e6a66f53ea69839d3ffdaf8481921d64a3596dd96989159ca3e023147b7 |
|
MD5 | ed13b52a8ff9e9235c20572a6bedafdd |
|
BLAKE2b-256 | e99de17f4d6a0b86c626378c49a1e925b2dfea4860d9a208e32f04e72a4a1668 |
Hashes for ngrok-0.10.1-cp37-abi3-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 010249031e5966aaf22f7ce669cc1c6b451987519303027f48bd95f44e5b172f |
|
MD5 | 36d076d919b72a7dcbc770bf683cf805 |
|
BLAKE2b-256 | 981d85f8db15389001fe974d469f5507ba81cea27bdbfddb0f8df766fe30d389 |