View random ngrok URL when run in background

前端 未结 12 1721
旧时难觅i
旧时难觅i 2021-02-02 08:16

When I start an ngrok client with ./ngrok tcp 22 it runs in the foreground and I can see the randoming generated forwarding URL, such as tcp://0.tcp.ngrok.io:

12条回答
  •  悲哀的现实
    2021-02-02 08:36

    import json
    import requests
    
    
    def get_ngrok_url():
        url = "http://localhost:4040/api/tunnels/"
        res = requests.get(url)
        res_unicode = res.content.decode("utf-8")
        res_json = json.loads(res_unicode)
        for i in res_json["tunnels"]:
            if i['name'] == 'command_line':
                return i['public_url']
                break
    

    This is an edit of JUN_NETWORKS python 3 code. It outputs the HTTPS URL only. I find Ngrok will randomly change the order of which is URL is displayed first sometimes outputting HTTP. The additional loop will consistently look for the 'tunnel' named 'command_line' which is the HTTPS URL.

提交回复
热议问题