I am trying to use RxPY to scan the IP to see which hosts are up.
However, currently it returns empty.
For ping(ip)
, if I simply return ip
, it will return a list of IP address.
from reactivex import operators as ops
import reactivex as rx
import subprocess
# If I change to this version, it will return a list of IP address.
# def ping(ip):
# return ip
def ping(ip):
retval = subprocess.call(["ping", "-c1", "-n", "-i0.1", "-W1", ip])
print("Here!") # It never reached here.
if retval == 0:
return ip # host is up
else:
return "" # host is down
if __name__ == "__main__":
ip_list = ["192.168.1.1", "192.168.1.2"]
rx.of(ip_list).pipe(
ops.map(lambda ip: ping(ip)),
).subscribe(lambda x: print(list(x)))
The line of subprocess.call
is totally skipped.
I am thinking it might be related with async, however in this case, the function subprocess.call
seems not an async function.
How to use subprocess
correctly? Any guide would be appreciate!