Search code examples
pythonpyngrok

Need API for dynamic URL mapping in python


i am working on a web server in python that uses ngrok (pyngrok). this generates new URLs every time i restart the code, making the URL redundant as i am constantly implementing changes. is there a workaround to this? now what i need is one master URL which redirects to another my generated URL that can be updated each time the URL is generated. let me know if my problem is not clear or if you know a solution


Solution

  • You don't specify the web framework you're using, which is pretty crucial to answer the question. However, I can still provide some clarity to help you understand why this is happening.

    Most popular frameworks come with a dev server, and this dev server forks itself when it starts. This is so one thread can manage the server and the other thread can watch for file changes–when a file change is detected, the thread managing the server gets kicked and restarted so your changes are immediately seen. You want pyngrok to run in the other thread, the parent thread, the one watching for file changes, as this thread is long-lived and does not get restarted (thus your ngrok URL will also be long-lived).

    The pyngrok documentation has several integration examples for (Flask, Django, and others) that show you how to start pyngrok in this way. You can find them here, but the short of it is, if you're using Flask you only want to start pyngrok when os.environ.get("WERKZEUG_RUN_MAIN") != "true", and if you're using Django you only want to start pyngrok when os.environ.get("RUN_MAIN", None) != "true".

    Let me know if it's another web framework that you're using and I can provide more examples.

    Full disclosure, I am the developer of pyngrok.