Search code examples
pythonpyngrok

Why doesn't pyngrok detect my config file?


I am trying to use my own configuration file with pyngrok but I don't understand why it does not detect it, my project needs to be run forcefully with sudo, therefore ngrok does not detect the configuration file in the root home directory for some users, it is that's why I want to mount my own configuration file in my project directory, here is my code:

from pyngrok import ngrok, conf
import os
from pathlib import Path
import re

def ngrok_start ():
        file = Path (".config/ngrok.yml")
        if file.exists():
                os.system("kill -9 $(pgrep ngrok)")
                ngrok.DEFAULT_CONFIG_PATH = ".config/ngrok.yml"
                ngrok.connect(443, "tcp")
                while True:
                        ngrok_tunnels = ngrok.get_tunnels()
                        url = ngrok_tunnels[0].public_url
                        if re.match ("tcp://[0-9]*.tcp.ngrok.io:[0-9]*", url) is not None:
                                print ("Ngrok TCP:" + url)
                                break

With my code I get the following error:

Traceback (most recent call last):
  File "ngrok.py", line 27, in <module>
    ngrok_start ()
  File "ngrok.py", line 20, in ngrok_start
    ngrok.connect (443, "tcp")
  File "/usr/local/lib/python2.7/dist-packages/pyngrok/ngrok.py", line 181, in connect
    timeout = pyngrok_config.request_timeout))
  File "/usr/local/lib/python2.7/dist-packages/pyngrok/ngrok.py", line 321, in api_request
    status_code, e.msg, e.hdrs, response_data)
pyngrok.exception.PyngrokNgrokHTTPError: ngrok client exception, API returned 502: {"error_code": 103, "status_code": 502, "msg": "failed to start tunnel", "details": {"err": "TCP tunnels are only available after you sign up. \ nSign up at: https://ngrok.com/signup\n\nIf you have already signed up, make sure your authtoken is installed. \ nYour authtoken is available on your dashboard: https: //dashboard.ngrok.com/auth/your-authtoken\r\n\r\nERR_NGROK_302\r\n "}}
No handlers could be found for logger "pyngrok.process"

My script is in the lib folder and my configuration file in .config/ngrok.yml, in which I have my token but I can't detect it, I hope you can support me, thanks.

imagen


Solution

  • Per the docs, the DEFAULT_CONFIG_PATH variable is in the conf module, not the ngrok module. So change ngrok.DEFAULT_CONFIG_PATH = to conf.get_default().config_path = .