1

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.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245

1 Answers1

1

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))

alexdlaird
  • 1,174
  • 12
  • 34
  • but how can i get the link ? – swanenjoyer Feb 28 '23 at 16:30
  • The [connect()](https://pyngrok.readthedocs.io/en/latest/api.html#pyngrok.ngrok.connect) method returns a `NgrokTunnel`, which has a `public_url` field. I updated the example to show how to print this. – alexdlaird Feb 28 '23 at 18:26