2015-09-24 50 views
0

我想弄清楚我是否可以通過檢查它的運行情況來管理我已安裝的程序。所以現在我正在使用這個腳本作爲我的基礎來監視我的本地應用程序。監測每個客戶端的應用程序過程

import os 
import sys 
import subprocess 
import time 

while True: 
     time.sleep(1) 
     if 'Python_Script.exe' not in subprocess.Popen('tasklist', stdout=subprocess.PIPE).communicate()[0]:     
       print 'Application was closed' 

注意:這是我的本地應用程序,我的目標是檢查每個客戶的狀態,例如, (192.168.21.2,192.168.21.3,192.168.21.4,192.168.21.5),

+0

那麼你的問題是什麼?這個代碼不工作嗎? –

+0

此代碼正在工作,我的觀點是隻有本地(我的電腦),我的目標是通過網絡。就像簡單的通過網絡ping,但這次我需要檢查我的應用程序是否爲每個客戶端運行 –

+0

首先,您必須在其他計算機上運行此程序。然後,如果你想將輸出發送到你的計算機,你應該使用[socket](https://docs.python.org/2/library/socket.html)。 –

回答

0

首先,你應該編寫一個程序,可以將輸出發送到你的計算機。我們推薦socket。下面是一個簡單的程序,可以發送一些文字到服務器:

from socket import * 

def server(): 
    try: 
     s = socket(AF_INET, SOCK_DGRAM) 
     port = int(input('Enter the port: ')) 

     s.bind(('', port)) 

     while True: 
      print('\nWaiting for a massage...') 
      msg = s.recvfrom(1024) 
      print(msg) 

    except KeyboardInterrupt: 
     f.close 
     print('\n\nServer exit.') 


def client(): 
    s = socket(AF_INET, SOCK_DGRAM) 

    host = str(input('\nEnter the server\'s IP: ')) 
    port = int(input('Enter the port: ')) 

    s.sendto("Here is the text that you'd like send to the server")\ 
    .encode('UTF-8'), (host, port)) 

    print('\nThe massage has been send.') 
  1. 這是一個Python 3程序。也許你應該把它轉換成Python 2.
  2. 你可以編寫你的程序作爲函數,然後return 'Application was closed'然後發送它。

但是,這只是一個例子,您需要自己編寫完整的程序。祝你好運:)