服务器 ping - 在哪里以及如何修复 TypeError "argument of type 'classname' is not iterable"?
server ping - Where and how to fix the TypeError "argument of type 'classname' is not iterable"?
在测试 here 处发布的服务器 ping 脚本时,我 运行 出现错误:
TypeError: argument of type 'MyCOM' is not iterable
查看解析为 subprocess.list2cmdline(seq)
的内容:已检查 seq:
parse result: ['ping', '-n', '192.168.0.2', '-w', '1000', <main.MyCOM object at 0x000002D26267F678>]
MyCOM 是类名。应该修复什么,如何以及在哪里工作?完整回溯:见下文。
我的 GUI 脚本:
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))
完整的回溯:
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()]
要解决这种(显然是继承)类型的错误,应替换以下行:
def ping(host_or_ip, packets=1, timeout=1000):
来自
def ping(self, host_or_ip, packets=1, timeout=1000):
提供 self
作为第一个参数,否则继承将不正确并且提到的错误将返回给您。
在测试 here 处发布的服务器 ping 脚本时,我 运行 出现错误:
TypeError: argument of type 'MyCOM' is not iterable
查看解析为 subprocess.list2cmdline(seq)
的内容:已检查 seq:
parse result: ['ping', '-n', '192.168.0.2', '-w', '1000', <main.MyCOM object at 0x000002D26267F678>]
MyCOM 是类名。应该修复什么,如何以及在哪里工作?完整回溯:见下文。
我的 GUI 脚本:
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))
完整的回溯:
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()]
要解决这种(显然是继承)类型的错误,应替换以下行:
def ping(host_or_ip, packets=1, timeout=1000):
来自
def ping(self, host_or_ip, packets=1, timeout=1000):
提供 self
作为第一个参数,否则继承将不正确并且提到的错误将返回给您。