0

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()
alexdlaird
  • 1,174
  • 12
  • 34
kalalua
  • 35
  • 4
  • That's a list of *instances* of the class. You are getting the string representation of the list, which includes the string representation of the instances. Check the documentation, as it should explain how to get the URL from the instance. – chepner Sep 08 '21 at 13:06
  • Read it whole.. no luck!! – kalalua Sep 08 '21 at 13:16
  • What is the code that *generates* this output? Please provide a [mcve]. – chepner Sep 08 '21 at 13:30
  • @chepner I have edited my question showing the code – kalalua Sep 09 '21 at 04:53

2 Answers2

1

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)
    
chepner
  • 497,756
  • 71
  • 530
  • 681
0

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

StuCode
  • 86
  • 6
  • No actually these are not objects even though I tried what you say, i get this `TypeError: 'NgrokTunnel' object is not iterable` because its type is `` – kalalua Sep 08 '21 at 13:16
  • Please add further details to expand on your answer, such as working code or documentation citations. – Community Sep 08 '21 at 13:26
  • have you tried doing just ngrok.public_url ? – StuCode Sep 08 '21 at 13:27
  • `url = ngrok.connect(5000,"http")` `url.public_url` but this just gives the http link not https.... I guess I'll need regex to take the url out – kalalua Sep 09 '21 at 05:07