using ngrok with python

702 Views Asked by At

How can I start ngrok with python and copy the public link just executing python script ?

I tried with subprocess but it only launched the 'ngrok http 80' command , I want to copy the public link.

1

There are 1 best solutions below

2
alexdlaird On

Use the pyngrok library, which you can install with pip install pyngrok or put in your requirements file. Then open a tunnel and grab the public URL you're asking for like this:

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()
print(" * ngrok tunnel \"{}\" -> \"http://127.0.0.1\"".format(http_tunnel.public_url))