import os import time from subprocess import Popen devnull = open(os.devnull, 'wb') p = [] # ip -> process for n in range(1, 10): # start ping processes ip = "192.168.0.%d" % n # p.append((ip, Popen(['ping', '-c', '3', ip], stdout=devnull))) p.append((ip, Popen(['ping',ip], stdout=devnull))) #NOTE: you could set stderr=subprocess.STDOUT to ignore stderr also while p: for i, (ip, proc) in enumerate(p[:]): if proc.poll() is not None: # ping finished p.remove((ip, proc)) # this makes it O(n**2) if proc.returncode == 0: print('%s active' % ip) elif proc.returncode == 2: print('%s no response' % ip) else: print('%s error' % ip) time.sleep(.04) devnull.close()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question