2017-02-18 31 views
0

我做了一個腳本,作爲業力和slimerjs laucher。如何讓python3x從karma中讀取終端輸出?

它是這樣的:

# Load a Karma server 
     print('\nKarma is loading...\n') 
     karma = subprocess.Popen('./karma start', shell=True) 

     # delays for 2 seconds for waiting Karma server 
     time.sleep(2) 

     # Load a headless SlimerJS that points to Karma server 
     slimerjs = subprocess.Popen(
      'xvfb-run slimerjs slimerjs-cfg.js > slimerjs.log', shell=True) 
     print('\nSlimerJS is running...\n') 

它的工作原理,但有一個很大的問題,這一點,2秒的任意時間後。我需要它在業力真正加載後才調用slimerjs,這就是爲什麼我使用延遲。 但因緣讓我這樣的控制檯輸出:

18 02 2017 03:11:12.176:WARN [karma]: No captured browser, open http://localhost:9876/ 

18 02 2017 03:11:12.190:INFO [karma]: Karma v1.3.0 server started at http://localhost:9876/ 

所以,我可以用這個輸出來代替2秒的任意時間後,我也需要得到的輸出知道在哪裏的業力開始

我需要得到服務器路徑/位置(http://localhost:9876/)來表示業務正在運行的slimerjs。

我tryed一些像這樣的命令:

p = subprocess.Popen(["./karma start"], stdout=subprocess.PIPE) 
     out, err = p.communicate() 

但是Python給我一個錯誤信息:

FileNotFoundError: [Errno 2] No such file or directory: './karma start' 

任何幫助嗎?

回答

1

沒有shell=True您需要的Popen調用這個樣子:

p = subprocess.Popen(["./karma", "start"], stdout=subprocess.PIPE) 

也得到網站:

location = p.stdout.split(' ')[-1] 

如果您正在使用Python版本> = 3.5,那麼你可以使用subprocess.run更新的更高版本的Popen。 Doc here