I am working on a script that would allow me to ping multiple hosts to check their status. Currently I am getting 2 errors while running the below script which I've found online. I am more of a network engineer rather than programmer. I have just started learning python but basics don't help me to fix this issue. Thanks in advance for your help
Traceback (most recent call last): File "/Users/p.petryszen/Documents/VsCode/Scripts/ping_hosts/ping_hosts.py", line 45, in pending = Queue.Queue() AttributeError: type object 'Queue' has no attribute 'Queue'
Traceback (most recent call last): File "/Users/p.petryszen/Documents/VsCode/Scripts/ping_hosts/ping_hosts.py", line 45, in pending = Queue.Queue() AttributeError: type object 'Queue' has no attribute 'Queue'
import sys
import os
import platform
import subprocess
from queue import Queue
import threading
def worker_func(pingArgs, pending, done):
try:
while True:
# Get the next address to ping.
address = pending.get_nowait()
ping = subprocess.Popen(ping_args + [address],
stdout = subprocess.PIPE,
stderr = subprocess.PIPE
)
out, error = ping.communicate()
# Output the result to the 'done' queue.
done.put((out, error))
except Queue.Empty:
# No more addresses.
pass
finally:
# Tell the main thread that a worker is about to terminate.
done.put(None)
# The number of workers.
NUM_WORKERS = 4
plat = platform.system()
scriptDir = sys.path[0]
hosts = os.path.join(scriptDir, 'hosts.txt')
# The arguments for the 'ping', excluding the address.
if plat == "Windows":
pingArgs = ["ping", "-n", "1", "-l", "1", "-w", "100"]
elif plat == "Linux":
pingArgs = ["ping", "-c", "1", "-l", "1", "-s", "1", "-W", "1"]
else:
raise ValueError("Unknown platform")
# The queue of addresses to ping.
pending = Queue.Queue()
# The queue of results.
done = Queue.Queue()
# Create all the workers.
workers = []
for _ in range(NUM_WORKERS):
workers.append(threading.Thread(target=worker_func, args=(pingArgs, pending, done)))
# Put all the addresses into the 'pending' queue.
with open(hosts, "r") as hostsFile:
for line in hostsFile:
pending.put(line.strip())
# Start all the workers.
for w in workers:
w.daemon = True
w.start()
# Print out the results as they arrive.
numTerminated = 0
while numTerminated < NUM_WORKERS:
result = done.get()
if result is None:
# A worker is about to terminate.
numTerminated += 1
else:
print(result[0]) # out
print(result[1]) # error
# Wait for all the workers to terminate.
for w in workers:
w.join()
With from queue import Queue
you imported only the object Queue in your script, so if you want to create a new instance of a queue you can directly use Queue()
, this means you would have:
from queue import Queue
# The queue of addresses to ping.
pending = Queue()
# The queue of results.
done = Queue()
If you instead import module queue as a whole you would need to specify where the object Queue comes from
import queue
# The queue of addresses to ping.
pending = queue.Queue()
# The queue of results.
done = queue.Queue()
For more references in modules:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.