一次运行多个.exe文件python

2024-04-27 22:58:40 发布

您现在位置:Python中文网/ 问答频道 /正文

如何在Python中同时运行多个.exe文件?我改编了另一个堆栈溢出问题的代码来生成LAN pinger。这个pinger必须ping子网掩码内的所有设备,所以在我的例子中它必须运行ping.exe文件255次。因此,运行此程序需要很长时间。我怎么跑ping.exe文件一次多次?在

我当前使用的代码如下:

import subprocess
import os
with open(os.devnull, "wb") as limbo:
        print "SCANNING YOUR LAN..."
        for n in xrange(1, 256):
                ip="192.168.0.{0}".format(n)
                result=subprocess.Popen(["ping", "-n", "1", "-w", "200", ip],
                        stdout=limbo, stderr=limbo).wait()
                if result:
                        pass
                else:
                        print ip, "is active"

我怎样才能使这个程序更有效率?在


Tags: 文件代码import程序ip堆栈osresult
2条回答

如果你有一个文件叫做IP地址.txt“有了这些IP,你可以像我想象的那样:

    f = open('ipaddress.txt')
    lines = f.readlines()
    f.close()
    for line in lines:
        subprocess.Popen(["ping", "-a", "-n", "l"]

即使代码不起作用,它仍然应该是一个概念。您需要ping 192.168.0.1/255,并将活动IP显示回来。在

不要在您的Popen调用中放入wait。在

如果您需要它们并行运行,但要等到它们全部完成后再继续,请执行以下操作:

# Create a list of the running processes
running = [subprocess.Popen(...) for ip in ips]
# Wait /after/ all process have launched. 
[process.wait() for process in running]
# Rest of code here.

当然,你必须重新制定一些东西,使ips列表,等等

相关问题 更多 >