我正在尝试创建一个python脚本,它报告我0.tcp.ngrok.io上的端口是在终端上运行代码时启动的(在将ngrok可执行文件移动到/ usr / local / bin之后)
ngrok tcp 22
我得到了那种输出
ngrok by @inconshreveable (Ctrl+C to quit)
Session Status connecting
Version 2.2.4
Region United States (us)
Web Interface http://127.0.0.1:4041
Forwarding tcp://0.tcp.ngrok.io:13014 -> localhost:22
Connections ttl opn rt1 rt5 p50 p90
0 0 0.00 0.00 0.00 0.00
我的第一次尝试是将子进程stdout记录到一个变量,但由于stdout是循环的,stdout.read()永远不会结束这就是代码
import subprocess
ngrok = subprocess.Popen(['ngrok','tcp','22'],stdout = subprocess.PIPE)
output_text = ngrok.stdout.read() # script stops here forever
[**code for getting domain:port from output_text**]
如何在不停止ngrok的情况下获取stdout到变量的“快照”?
有没有其他方法可以做到这一点(下一次尝试将是localhost上的webscrapper,但是对于其他命令,例如“top”),这将是很好的。
提前致谢
答案 0 :(得分:0)
当我使用ngrok http
并且所有替代方法都无法正常工作时,我遇到了同样的问题,导致死锁,我甚至可以打印使用ngrok创建的子进程响应。因此,阅读ngrok docs我注意到有一种方法可以获得有关请求的ngrok公共URL。
添加以下代码:
localhost_url = "http://localhost:4041/api/tunnels" #Url with tunnel details
tunnel_url = requests.get(localhost_url).text #Get the tunnel information
j = json.loads(tunnel_url)
tunnel_url = j['Tunnels'][0]['PublicUrl'] #Do the parsing of the get
因此,tunnel_url将返回您需要的内容。添加导入完整代码将是这样的:
import subprocess
import requests
import json
ngrok = subprocess.Popen(['ngrok','tcp','22'],stdout = subprocess.PIPE)
localhost_url = "http://localhost:4041/api/tunnels" #Url with tunnel details
tunnel_url = requests.get(localhost_url).text #Get the tunnel information
j = json.loads(tunnel_url)
tunnel_url = j['Tunnels'][0]['PublicUrl'] #Do the parsing of the get
答案 1 :(得分:0)
没有足够的声誉来评论,随时更新或评论@Rodolfo很棒的答案然后删除
也许他们稍微改变了api,这对我有用: (脚本旁边的ngrok可执行文件,在端口5000上提供http并选择https隧道URL)
import subprocess
import requests
import json
import time
if __name__ == '__main__':
ngrok = subprocess.Popen(['./ngrok','http','5000'],
stdout=subprocess.PIPE)
time.sleep(3) # to allow the ngrok to fetch the url from the server
localhost_url = "http://localhost:4040/api/tunnels" #Url with tunnel details
tunnel_url = requests.get(localhost_url).text #Get the tunnel information
j = json.loads(tunnel_url)
tunnel_url = j['tunnels'][1]['public_url'] #Do the parsing of the get
print(tunnel_url)
答案 2 :(得分:0)
这对我有用:
def open_tunnel():
process = subprocess.Popen(f'/snap/bin/ngrok tcp {PORT} --log "stdout"', shell=True, stdout=PIPE, stderr=PIPE) # You can also use a list and put shell = False
while True:
output = process.stdout.readline()
if not output and process.poll() is not None:
break
elif b'url=' in output:
output = output.decode()
output = output[output.index('url=tcp://') + 10 : -1]
return output.split(':')
我使用/snap/bin/ngrok
是因为我的pycharm无法识别路径,对此感到抱歉。您可以只说ngrok
来代替它