Python Ngrok get active ngrok agent session for use in Flask App

Viewed 316

I am writing a Flask app that filters HTTP requests through an Ngrok tunnel. Everything works fine when I hard code the tunnel URL. The problem presented itself when I tried to introduce some automation logic to my program that checks if the there is an active tunnel with: ngrok.get_tunnels()

My plan was to establish a new connection and update my notification URL in the event of a missing active connection. Unfortunately, I can't even get to that step because my program Errors out with the message:

Your account is limited to 1 simultaneous ngrok agent session.\nActive ngrok agent sessions in region 'us'

This error occurs on ngrok.get_tunnels()

I've tried killing the ngrok.exe process, but the error still occurs as soon as my app call my ngrok function.

I am looking for a method to get the agent session to use in my program so a new session doesn't attempt to start if one is already active.

This is the logic I am trying to implement:

def tunnel_host():
    active_tunnels = ngrok.get_tunnels()
    if not active_tunnels:
        tunnel = ngrok.connect(5000, bind_tls=True)
        tunnel_url = tunnel.public_url
        return tunnel_url
    else:
        tunnel = ngrok.get_tunnels()
        tunnel_url = tunnel[0].public_url
        return tunnel_url

I greatly appreciate any feedback.

0 Answers
Related