2017-07-02 84 views
0

首先,我知道有很多方法來捕捉一個shell的輸出...我發現this和正常工作:捕獲殼調試過程的輸出

from subprocess import Popen, PIPE 

with Popen(["ping", "localhost"], stdout=PIPE, bufsize=1, universal_newlines=True) as p: 
     for line in p.stdout: 
      if "bytes" in line: 
       print(line, end='') 

但我需要得到調試從德CMD信息,而這也不行...... 我想從geth拋出像一些信息的輸出:

[email protected]:~$ geth --light 
INFO [07-02|21:56:08] Starting peer-to-peer node    
INFO [07-02|21:56:08] Allocated cache and file handles   
INFO [07-02|21:56:08] Initialised chain configuration   
INFO [07-02|21:56:08] Disk storage enabled for ethash caches 
INFO [07-02|21:56:08] Disk storage enabled for ethash DAGs  
INFO [07-02|21:56:08] Added trusted CHT for mainnet 
INFO [07-02|21:56:08] Loaded most recent local header 

這是調試信息的話,我不知道爲什麼,但我無法正常捕捉到它。我需要這樣的東西:

with Popen(["geth", "--light"], stdout=PIPE, bufsize=1, universal_newlines=True) as p: 
     for line in p.stdout: 
      if "Loaded" in line: 
       print("NICE!!") 

有沒有辦法做到這一點?

+0

通常調試消息是標準錯誤。嘗試用'stderr = PIPE'捕捉stderr,然後用'p.stderr' – kwarunek

+0

好!非常感謝 – pctripsesp

回答

0

由於kwarunek的答案,它的工作

通常調試消息是標準錯誤。嘗試捕捉標準錯誤與 標準錯誤=管道,然後p.stderr

from subprocess import Popen, PIPE 

with Popen(["geth", "--light"], stderr=PIPE, bufsize=1, universal_newlines=True) as p: 
    for line in p.stderr: 
     if "Block synchronisation started" in line: 
      print(line, end='')