API Documentation¶
This part of the documentation covers the interfaces used to develop with pyngrok.
Main ngrok Interface¶
-
class
pyngrok.ngrok.NgrokTunnel(data, pyngrok_config, api_url)[source]¶ Bases:
objectAn object containing information about a
ngroktunnel.- Variables
data (dict) – The original tunnel data.
name (str) – The name of the tunnel.
proto (str) – A valid tunnel protocol.
uri (str) – The tunnel URI, a relative path that can be used to make requests to the
ngrokweb interface.public_url (str) – The public
ngrokURL.config (dict) – The config for the tunnel.
metrics (dict) – Metrics for the tunnel.
pyngrok_config (PyngrokConfig) – The
pyngrokconfiguration to use when interacting with thengrok.api_url (str) – The API URL for the
ngrokweb interface.
-
pyngrok.ngrok.api_request(url, method='GET', data=None, params=None, timeout=4)[source]¶ Invoke an API request to the given URL, returning JSON data from the response.
One use for this method is making requests to
ngroktunnels:from pyngrok import ngrok public_url = ngrok.connect() response = ngrok.api_request("{}/some-route".format(public_url), method="POST", data={"foo": "bar"})
Another is making requests to the
ngrokAPI itself:from pyngrok import ngrok api_url = ngrok.get_ngrok_process().api_url response = ngrok.api_request("{}/api/requests/http".format(api_url), params={"tunnel_name": "foo"})
-
pyngrok.ngrok.connect(addr=None, proto=None, name=None, pyngrok_config=None, **options)[source]¶ Establish a new
ngroktunnel for the given protocol to the given port, returning an object representing the connected tunnel.If a tunnel definition in ngrok’s config file matches the given
name, it will be loaded and used to start the tunnel. WhennameisNoneand a “pyngrok-default” tunnel definition exists inngrok’s config, it will be loaded and use. Anykwargspassed asoptionswill override properties from the loaded tunnel definition.If
ngrokis not installed atPyngrokConfig’sngrok_path, calling this method will first download and installngrok.If
ngrokis not running, calling this method will first start a process withPyngrokConfig.Note
ngrok’s default behavior forhttpwhen no additional properties are passed is to open two tunnels, onehttpand onehttps. This method will return a reference to thehttptunnel in this case. If only a single tunnel is needed, passbind_tls=True.- Parameters
addr (str, optional) – The local port to which the tunnel will forward traffic, or a local directory or network address, defaults to “80”.
proto (str, optional) – The protocol to tunnel, defaults to “http”.
name (str, optional) – A friendly name for the tunnel, or the name of a ngrok tunnel definition to be used.
pyngrok_config (PyngrokConfig, optional) – A
pyngrokconfiguration to use when interacting with thengrokbinary, overridingget_default.options (dict, optional) – Remaining
kwargsare passed as configuration for the ngrok tunnel.
- Returns
The created
ngroktunnel.- Return type
-
pyngrok.ngrok.disconnect(public_url, pyngrok_config=None)[source]¶ Disconnect the
ngroktunnel for the given URL, if open.- Parameters
public_url (str) – The public URL of the tunnel to disconnect.
pyngrok_config (PyngrokConfig, optional) – A
pyngrokconfiguration to use when interacting with thengrokbinary, overridingget_default.
-
pyngrok.ngrok.get_ngrok_process(pyngrok_config=None)[source]¶ Get the current
ngrokprocess for the given config’sngrok_path.If
ngrokis not installed atPyngrokConfig’sngrok_path, calling this method will first download and installngrok.If
ngrokis not running, calling this method will first start a process withPyngrokConfig.Use
is_process_runningto check if a process is running without also implicitly installing and starting it.- Parameters
pyngrok_config (PyngrokConfig, optional) – A
pyngrokconfiguration to use when interacting with thengrokbinary, overridingget_default.- Returns
The
ngrokprocess.- Return type
-
pyngrok.ngrok.get_tunnels(pyngrok_config=None)[source]¶ Get a list of active
ngroktunnels for the given config’sngrok_path.If
ngrokis not installed atPyngrokConfig’sngrok_path, calling this method will first download and installngrok.If
ngrokis not running, calling this method will first start a process withPyngrokConfig.- Parameters
pyngrok_config (PyngrokConfig, optional) – A
pyngrokconfiguration to use when interacting with thengrokbinary, overridingget_default.- Returns
The active
ngroktunnels.- Return type
-
pyngrok.ngrok.get_version(pyngrok_config=None)[source]¶ Get a tuple with the
ngrokandpyngrokversions.- Parameters
pyngrok_config (PyngrokConfig, optional) – A
pyngrokconfiguration to use when interacting with thengrokbinary, overridingget_default.- Returns
A tuple of
(ngrok_version, pyngrok_version).- Return type
-
pyngrok.ngrok.install_ngrok(pyngrok_config=None)[source]¶ Download, install, and initialize
ngrokfor the given config. Ifngrokand its default config is already installed, calling this method will do nothing.- Parameters
pyngrok_config (PyngrokConfig, optional) – A
pyngrokconfiguration to use when interacting with thengrokbinary, overridingget_default.
-
pyngrok.ngrok.kill(pyngrok_config=None)[source]¶ Terminate the
ngrokprocesses, if running, for the given config’sngrok_path. This method will not block, it will just issue a kill request.- Parameters
pyngrok_config (PyngrokConfig, optional) – A
pyngrokconfiguration to use when interacting with thengrokbinary, overridingget_default.
-
pyngrok.ngrok.main()[source]¶ Entry point for the package’s
console_scripts. This initializes a call from the command line and invokesrun.This method is meant for interacting with
ngrokfrom the command line and is not necessarily compatible with non-blocking API methods. For that, usengrok’s interface methods (likeconnect), or useget_process.
-
pyngrok.ngrok.run(args=None, pyngrok_config=None)[source]¶ Ensure
ngrokis installed at the default path, then callrun_process.This method is meant for interacting with
ngrokfrom the command line and is not necessarily compatible with non-blocking API methods. For that, usengrok’s interface methods (likeconnect), or useget_process.- Parameters
args (list[str], optional) – Arguments to be passed to the
ngrokprocess.pyngrok_config (PyngrokConfig, optional) – A
pyngrokconfiguration to use when interacting with thengrokbinary, overridingget_default.
-
pyngrok.ngrok.set_auth_token(token, pyngrok_config=None)[source]¶ Set the
ngrokauth token in the config file, enabling authenticated features (for instance, more concurrent tunnels, custom subdomains, etc.).If
ngrokis not installed atPyngrokConfig’sngrok_path, calling this method will first download and installngrok.- Parameters
token (str) – The auth token to set.
pyngrok_config (PyngrokConfig, optional) – A
pyngrokconfiguration to use when interacting with thengrokbinary, overridingget_default.
-
pyngrok.ngrok.update(pyngrok_config=None)[source]¶ Update
ngrokfor the given config’sngrok_path, if an update is available.- Parameters
pyngrok_config (PyngrokConfig, optional) – A
pyngrokconfiguration to use when interacting with thengrokbinary, overridingget_default.- Returns
The result from the
ngrokupdate.- Return type
Process Management¶
-
class
pyngrok.process.NgrokLog(line)[source]¶ Bases:
objectAn object containing a parsed log from the
ngrokprocess.
-
class
pyngrok.process.NgrokProcess(proc, pyngrok_config)[source]¶ Bases:
objectAn object containing information about the
ngrokprocess.- Variables
proc (subprocess.Popen) – The child process that is running
ngrok.pyngrok_config (PyngrokConfig) – The
pyngrokconfiguration to use withngrok.api_url (str) – The API URL for the
ngrokweb interface.logs (list[NgrokLog]) – A list of the most recent logs from
ngrok, limited in size tomax_logs.startup_error (str) – If
ngrokstartup fails, this will be the log of the failure.
-
_log_line(line)[source]¶ Parse, log, and emit (if
log_event_callbackinPyngrokConfigis registered) the given log line.
-
_log_startup_line(line)[source]¶ Parse the given startup log line and use it to manage the startup state of the
ngrokprocess.
-
healthy()[source]¶ Check whether the
ngrokprocess has finished starting up and is in a running, healthy state.- Returns
Trueif thengrokprocess is started, running, and healthy.- Return type
-
start_monitor_thread()[source]¶ Start a thread that will monitor the
ngrokprocess and its logs until it completes.If a monitor thread is already running, nothing will be done.
-
stop_monitor_thread()[source]¶ Set the monitor thread to stop monitoring the
ngrokprocess after the next log event. This will not necessarily terminate the thread immediately, as the thread may currently be idle, rather it sets a flag on the thread telling it to terminate the next time it wakes up.This has no impact on the
ngrokprocess itself, onlypyngrok’s monitor of the process and its logs.
-
pyngrok.process._start_process(pyngrok_config)[source]¶ Start a
ngrokprocess with no tunnels. This will start thengrokweb interface, against which HTTP requests can be made to create, interact with, and destroy tunnels.- Parameters
pyngrok_config (PyngrokConfig) – The
pyngrokconfiguration to use when interacting with thengrokbinary.- Returns
The
ngrokprocess.- Return type
-
pyngrok.process._validate_path(ngrok_path)[source]¶ Validate the given path exists, is a
ngrokbinary, and is ready to be started, otherwise raise a relevant exception.- Parameters
ngrok_path (str) – The path to the
ngrokbinary.
-
pyngrok.process.capture_run_process(ngrok_path, args)[source]¶ Start a blocking
ngrokprocess with the binary at the given path and the passed args. When the process returns, so will this method, and the captured output from the process along with it.This method is meant for invoking
ngrokdirectly (for instance, from the command line) and is not necessarily compatible with non-blocking API methods. For that, useget_process.
-
pyngrok.process.get_process(pyngrok_config)[source]¶ Get the current
ngrokprocess for the given config’sngrok_path.If
ngrokis not running, calling this method will first start a process withPyngrokConfig.- Parameters
pyngrok_config (PyngrokConfig) – The
pyngrokconfiguration to use when interacting with thengrokbinary.- Returns
The
ngrokprocess.- Return type
-
pyngrok.process.is_process_running(ngrok_path)[source]¶ Check if the
ngrokprocess is currently running.- Parameters
ngrok_path (str) – The path to the
ngrokbinary.- Returns
Trueifngrokis running from the given path.
-
pyngrok.process.kill_process(ngrok_path)[source]¶ Terminate the
ngrokprocesses, if running, for the given path. This method will not block, it will just issue a kill request.- Parameters
ngrok_path (str) – The path to the
ngrokbinary.
-
pyngrok.process.run_process(ngrok_path, args)[source]¶ Start a blocking
ngrokprocess with the binary at the given path and the passed args.This method is meant for invoking
ngrokdirectly (for instance, from the command line) and is not necessarily compatible with non-blocking API methods. For that, useget_process.
-
pyngrok.process.set_auth_token(pyngrok_config, token)[source]¶ Set the
ngrokauth token in the config file, enabling authenticated features (for instance, more concurrent tunnels, custom subdomains, etc.).- Parameters
pyngrok_config (PyngrokConfig) – The
pyngrokconfiguration to use when interacting with thengrokbinary.token (str) – The auth token to set.
Configuration¶
-
class
pyngrok.conf.PyngrokConfig(ngrok_path=None, config_path=None, auth_token=None, region=None, monitor_thread=True, log_event_callback=None, startup_timeout=15, max_logs=100, request_timeout=4, start_new_session=False)[source]¶ Bases:
objectAn object containing
pyngrok’s configuration for interacting with thengrokbinary. All values are optional when it is instantiated, and default values will be used for parameters not passed.Use
get_defaultandset_defaultto interact with the defaultpyngrok_config, or pass another instance of this object as thepyngrok_configkeyword arg to most methods in thengrokmodule to override the default.from pyngrok import conf, ngrok # Here we update the entire default config pyngrok_config = conf.PyngrokConfig(ngrok_path="/usr/local/bin/ngrok") conf.set_default(pyngrok_config) # Here we update just one variable in the default config conf.get_default().ngrok_path = "/usr/local/bin/ngrok" # Here we leave the default config as-is and pass an override pyngrok_config = conf.PyngrokConfig(ngrok_path="/usr/local/bin/ngrok") ngrok.connect(pyngrok_config=pyngrok_config)
- Variables
ngrok_path (str) – The path to the
ngrokbinary, defaults to the value in conf.DEFAULT_NGROK_PATHconfig_path (str) – The path to the
ngrokconfig, defaults toNoneandngrokmanages it.auth_token (str) – An authtoken to pass to commands (overrides what is in the config).
region (str) – The region in which
ngrokshould start.monitor_thread (bool) – Whether
ngrokshould continue to be monitored (for logs, etc.) after it startup is complete.log_event_callback (types.FunctionType) – A callback that will be invoked each time
ngrokemits a log.monitor_threadmust be set toTrueor the function will stop being called afterngrokfinishes starting.startup_timeout (int) – The max number of seconds to wait for
ngrokto start before timing out.max_logs (int) – The max number of logs to store in
NgrokProcess’slogsvariable.request_timeout (float) – The max timeout when making requests to
ngrok’s API.start_new_session (bool) – Passed to
subprocess.Popenwhen launchingngrok. (Python 3 and POSIX only)
-
pyngrok.conf.get_default()[source]¶ Get the default config to be used with methods in the
ngrokmodule. To override the default individually, thepyngrok_configkeyword arg can also be passed to most of these methods, or set a new default config withset_default.- Returns
The default
pyngrok_config.- Return type
-
pyngrok.conf.set_default(pyngrok_config)[source]¶ Set a new default config to be used with methods in the
ngrokmodule. To override the default individually, thepyngrok_configkeyword arg can also be passed to most of these methods.- Parameters
pyngrok_config (PyngrokConfig) – The new
pyngrok_configto be used by default.
ngrok Installer¶
-
pyngrok.installer._download_file(url, retries=0, **kwargs)[source]¶ Download a file to a temporary path and emit a status to stdout (if possible) as the download progresses.
- Parameters
url (str) – The URL to download.
retries (int, optional) – The number of retries to attempt, if download fails.
kwargs (dict, optional) – Remaining
kwargswill be passed tourllib.request.urlopen.
- Returns
The path to the downloaded temporary file.
- Return type
-
pyngrok.installer._install_ngrok_zip(ngrok_path, zip_path)[source]¶ Extract the
ngrokzip file to the given path.
-
pyngrok.installer.get_ngrok_bin()[source]¶ Get the
ngrokexecutable for the current system.- Returns
The name of the
ngrokexecutable.- Return type
-
pyngrok.installer.get_ngrok_config(config_path, use_cache=True)[source]¶ Get the
ngrokconfig from the given path.
-
pyngrok.installer.install_default_config(config_path, data=None)[source]¶ Install the given data to the
ngrokconfig. If a config is not already present for the given path, create one. Before saving new data to the default config, validate that they are compatible withpyngrok.
-
pyngrok.installer.install_ngrok(ngrok_path, **kwargs)[source]¶ Download and install the latest
ngrokfor the current system, overwriting any existing contents at the given path.- Parameters
ngrok_path (str) – The path to where the
ngrokbinary will be downloaded.kwargs (dict, optional) – Remaining
kwargswill be passed to_download_file.
Exceptions¶
-
exception
pyngrok.exception.PyngrokError[source]¶ Bases:
ExceptionRaised when a general
pyngrokerror has occurred.
-
exception
pyngrok.exception.PyngrokNgrokError(error, ngrok_logs=None, ngrok_error=None)[source]¶ Bases:
pyngrok.exception.PyngrokErrorRaised when an error occurs interacting directly with the
ngrokbinary.
-
exception
pyngrok.exception.PyngrokNgrokHTTPError(error, url, status_code, message, headers, body)[source]¶ Bases:
pyngrok.exception.PyngrokNgrokErrorRaised when an error occurs making a request to the
ngrokweb interface. Thebodycontains the error response received fromngrok.- Variables
error (str) – A description of the error being thrown.
url (str) – The request URL that failed.
status_code (int) – The response status code from
ngrok.message (str) – The response message from
ngrok.headers (dict[str, str]) – The request headers sent to
ngrok.body (str) – The response body from
ngrok.
-
exception
pyngrok.exception.PyngrokNgrokInstallError[source]¶ Bases:
pyngrok.exception.PyngrokErrorRaised when an error has occurred while downloading and installing the
ngrokbinary.
-
exception
pyngrok.exception.PyngrokNgrokURLError(error, reason)[source]¶ Bases:
pyngrok.exception.PyngrokNgrokErrorRaised when an error occurs when trying to initiate an API request.
-
exception
pyngrok.exception.PyngrokSecurityError[source]¶ Bases:
pyngrok.exception.PyngrokErrorRaised when a
pyngroksecurity error has occurred.