In Python pyngrok error for .replace method

803 views Asked by At

I get an error on this line:

link = ngrok.connect(4040,"http").replace("http","https")

Error:

Instance of 'NgrokTunnel' has no 'replace' member

2

There are 2 answers

1
SYNEC On BEST ANSWER

I've tested it.

Your link is no string. You have to convert it into a string in order to replace text.

This works with the function str().

link = str(ngrok.connect()).replace("http", "https")
0
alexdlaird On

The accepted answer is not quite correct, as the string you'll end up with is [<NgrokTunnel: "https://<public_sub>.ngrok.io" -> "http://localhost:80">] when the string you want is just the https://<public_sub>.ngrok.io part.

The NgrokTunnel object has a public_url attribute, which is what you want, so do this:

link = ngrok.connect(4040, "http").public_url.replace("http","https")

Moreover, if you don't even need the http port opened, this will just give you the https link by only opening a single tunnel, no need to manipulate the string:

link = ngrok.connect(4040, bind_tls=True).public_url

It's worth noting the accepted answer will work if you are using an older version of pyngrok (pre-5.0.0 release).