While testing a server ping script posted here at SO I ran into an error:
TypeError: argument of type 'MyCOM' is not iterable
To see what is parsed to subprocess.list2cmdline(seq)
: seq was checked:
parse result: ['ping', '-n', '192.168.0.2', '-w', '1000', <main.MyCOM object at 0x000002D26267F678>]
MyCOM is a classname. What, how and where should this be fixed to work? Full traceback: see below.
My GUI script:
class MainGUI(etc...):
def __init__(self, *args, parent=None):
...snippet...
self.mycom_thread = MyCOM()
self.mycom_thread.start()
...snippet...
class MyCOM(QThread):
def __init__(self, parent = None):
QThread.__init__(self, parent)
self.ping_okay = 0
def ping(host_or_ip, packets=1, timeout=1000):
''' Calls system "ping" command, returns True if ping succeeds.
Required parameter: host_or_ip (str, address of host to ping)
Optional parameters: packets (int, number of retries), timeout (int, ms to wait for response)
Does not show any output, either as popup window or in command line.
Python 3.5+, Windows and Linux compatible (Mac not tested, should work)
Credits to: SO user Jose Francisco Lopez Pimentel.
'''
# The ping command is the same for Windows and Linux, except for the "number of packets" flag.
if platform.system().lower() == 'windows':
print ('some text')
command = ['ping', '-n', str(packets), '-w', str(timeout), host_or_ip]
# run parameters: capture output, discard error messages, do not show window
result = subprocess.run(command, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, creationflags=0x08000000)
# 0x0800000 is a windows-only Popen flag to specify that a new process will not create a window.
# On Python 3.7+, you can use a subprocess constant:
# result = subprocess.run(command, capture_output=True, creationflags=subprocess.CREATE_NO_WINDOW)
# On windows 7+, ping returns 0 (ok) when host is not reachable; to be sure host is responding,
# we search the text "TTL=" on the command output. If it's there, the ping really had a response.
return result.returncode == 0 and b'TTL=' in result.stdout
else:
command = ['ping', '-c', str(packets), '-w', str(timeout), host_or_ip]
# run parameters: discard output and error messages
result = subprocess.run(command, stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
return result.returncode == 0
def run(self):
check = 1
IP = "192.168.0.2"
while check == 1:
print (self.ping(IP))
The full traceback:
Traceback (most recent call last): File "mymain.py", line 1156, in run print (self.ping(IP)) File "mymain.py", line 1137, in ping result = subprocess.run(command, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, creationflags=0x08000000) File "x\lib\subprocess.py", line 383, in run with Popen(*popenargs, **kwargs) as process: File "x\lib\subprocess.py", line 678, in __init__ restore_signals, start_new_session) File "x\lib\subprocess.py", line 933, in _execute_child args = list2cmdline(args) File "x\lib\subprocess.py", line 443, in list2cmdline needquote = (" " in arg) or ("\t" in arg) or not arg TypeError: argument of type 'MyCOM' is not iterable Exception ignored in: <module 'threading' from 'x\\lib\\threading.py'> Traceback (most recent call last): File "x\lib\threading.py", line 1294, in _shutdown _main_thread._delete() File "x\lib\threading.py", line 1013, in _delete del _active[get_ident()]