Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在Mac上安装子进程_Python_Python 3.x - Fatal编程技术网

Python 在Mac上安装子进程

Python 在Mac上安装子进程,python,python-3.x,Python,Python 3.x,我正在尝试执行此脚本,但它不会运行。我相信子进程并不是根据错误消息安装的。我正在Mac上使用python 3.9。我认为Python3的默认库中已经有子进程,但我可能错了。当我尝试执行pip或pip3安装子流程时,它也不起作用。我不知道该怎么做 #!/usr/local/bin/python3 import subprocess network = '10.21.30.' def hostList(number): hosts = [] count = 1 whil

我正在尝试执行此脚本,但它不会运行。我相信子进程并不是根据错误消息安装的。我正在Mac上使用python 3.9。我认为Python3的默认库中已经有子进程,但我可能错了。当我尝试执行pip或pip3安装子流程时,它也不起作用。我不知道该怎么做

#!/usr/local/bin/python3

import subprocess

network = '10.21.30.'

def hostList(number):
    hosts = []
    count = 1
    while (count <= number):
        hosts.append((network + str(count)))
        count += 1
    return hosts

def ping_test (host):

    reached = []                           #Empty list to collect reachable hosts
    not_reached = []                          #Empty list to collect unreachable hosts

    for ip in host:
        ping_test = subprocess.call('ping %s -n 2' % ip)        #Ping host n times
        if ping_test == 0:                    #If ping test is 0, it' reachable
            reached.append(ip)

        else:
            not_reached.append(ip)                              #Else, it's not reachable

    print("{} is reachable".format(reached))
 

#hosts = ["192.168.1.1","123.214.2.2","www.google.com",]
ping_test(hostList(254))
#/usr/local/bin/python3
导入子流程
网络='10.21.30'
def主机列表(编号):
主机=[]
计数=1

虽然(count错误有点神秘,但您希望

  • 将参数拆分到
    子流程。调用
    (或更好)到列表中,例如
subprocess.run([“ping”,“stackoverflow.com]”)

  • 添加
    shell=True
    选项
subprocess.run(“ping stackoverflow.com”,shell=True)
对于第二个选项,请务必阅读

Traceback (most recent call last):
  File "/Users/user/Documents/ex5.py", line 32, in <module>
    ping_test(hosts)
  File "/Users/user/Documents//ex5.py", line 21, in ping_test
    ping_test = subprocess.call('ping %s -n 2' % ip)        #Ping host n times
  File "/usr/local/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/subprocess.py", line 349, in call
    with Popen(*popenargs, **kwargs) as p:
  File "/usr/local/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/subprocess.py", line 951, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/usr/local/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/subprocess.py", line 1823, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'ping 192.168.1.1 -n 2'