How can I get output of class in custom formatted way python? (NGROK tunnel names)

268 Views Asked by At

I am using ngrok to ofcourse portforward my local system but when getting tunnels names ngrok.get_tunnels() it outputs in weird way so when I check the type of its output it shows that it is a class. Output like this

[<NgrokTunnel: "http://4527-124-123-122-140.ngrok.io" -> "http://localhost:3243">, <NgrokTunnel: "https://4527-124-123-122-140.ngrok.io" -> "http://localhost:3243">]

How can I take the URL names from it like I want in this way

HTTP tunnel: http://4527-124-123-122-140.ngrok.io HTTPS tunnel: https://4527-124-123-122-140.ngrok.io

Below is my code which is just 3 lines to give the output

from pyngrok install ngrok

ngrok.connect(5000, "http")
ngrok.get_tunnels()
2

There are 2 best solutions below

0
On

You have to save the return value of get_tunnels, so that you can iterate over it.

from pyngrok install ngrok

ngrok.connect(5000, "http")
tunnels = ngrok.get_tunnels()

for t in tunnels:
    print(t.public_url)
    
4
On

You should be able to do a loop through each of the ngrok objects and then do ngrok[i].public_url to get the url attribute of the class