2017-06-27 21 views
0

私は、ターミナルでコードを実行すると、0.tcp.ngrok.io上のポートが起動されたことをPythonスクリプトで報告しようとしています(ngrok実行ファイルを/ usr/local/binに)サブプロセスstdoutをNgrokの変数に置き換えます。

ngrok tcp 22

私は、出力のTHS種類

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.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の「スナップショット」をどのように取得できますか?

これを行うための別の方法(次の試行がローカルホスト上のwebscrapperだろうが、そのような「トップ」などの他のコマンドのためにこの知識を持っていいだろう)事前に

おかげ

答えて

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 
0

ないコメントするのに十分な評判を、更新または@Rodolfoにコメント偉大な答えをして、おそらく彼らは変更しました。この

を削除して自由に感じますちょっと、これは私のために働いたものです (スクリプトの隣に実行可能な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) 
関連する問題