【问题标题】:TypeError: can only concatenate list (not "int") to listTypeError:只能将列表(不是“int”)连接到列表
【发布时间】:2011-03-17 17:55:02
【问题描述】:

我正在尝试对以下代码中的一些项目做一些有用的事情:

IPS=['10.254.243.83','10.254.243.82']

def commands(cmd):
    command = Popen(cmd, shell=True, stdout=PIPE)
    command_strip = command.stdout.read().strip()
    print command_strip

def main():
    for ip in IPS:
        ping = call('ping -c 3 %s' % ip, shell=True)
        commands(ping)

if __name__ == "__main__":
    main()

然后,它返回给我: python teste.py

PING 10.254.243.83 (10.254.243.83) 56(84) bytes of data.
64 bytes from 10.254.243.83: icmp_seq=1 ttl=61 time=2.72 ms
64 bytes from 10.254.243.83: icmp_seq=2 ttl=61 time=2.05 ms
64 bytes from 10.254.243.83: icmp_seq=3 ttl=61 time=1.88 ms

--- 10.254.243.83 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2004ms
rtt min/avg/max/mdev = 1.885/2.224/2.728/0.363 ms

Traceback (most recent call last):
  File "teste.py", line 15, in ?
    main()
  File "teste.py", line 12, in main
    commands(ping)
  File "teste.py", line 5, in commands
    command = Popen(cmd, shell=True, stdout=PIPE)
  File "/usr/lib/python2.4/subprocess.py", line 543, in __init__
    errread, errwrite)
  File "/usr/lib/python2.4/subprocess.py", line 891, in _execute_child
    args = ["/bin/sh", "-c"] + args
TypeError: can only concatenate list (not "int") to list

Can someone help me with this error ?

【问题讨论】:

  • 如果这为您解决了问题,请随时为我们中的一个人提供答案。我的事情@Sven 在我之前几秒钟就得到了;)

标签: python


【解决方案1】:

你传递的返回值

call('ping -c 3 %s' % ip, shell=True)

作为cmd 函数的commands() 参数。上面提到的返回值是一个整数,它作为命令没有任何意义,所以尝试使用Popen() 执行这个整数会失败。

【讨论】:

  • 谢谢,我的目的是通过电子邮件将结果发送给某些人:)。我现在开始工作了!这就是 Popen 的原因
【解决方案2】:

您是否尝试过替换

ping = call('ping -c 3 %s' % ip, shell=True)

ping = 'ping -c 3 %s' % ip

?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-04
    • 1970-01-01
    相关资源
    最近更新 更多