gpt4 book ai didi

python - 操作系统错误 : [Errno 24] Too many open files: '/dev/null' or No IP is showing on my connected network

转载 作者:太空宇宙 更新时间:2023-11-04 04:16:39 24 4
gpt4 key购买 nike

我正在编写一个 python 脚本来检测连接到我的网络的 IP 地址。

当我在 Spyder Anaconda (MAC OS) 中运行脚本时,它运行完美。

但是当我在 Windows 中运行该程序时,它只打印主机 IP 地址,而不是所有连接的 IP 地址。

此外,当我在 MAC OS 中通过终端运行程序时,它再次显示错误

OSError: [Errno 24] Too many open files: '/dev/null'

知道我做错了什么吗?

我的代码:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Mar 21 09:46:11 2019

@author: vivanksharma
"""

import os
import socket
import multiprocessing
import subprocess
import os


def pinger(job_q, results_q):
"""
Do Ping
:param job_q:
:param results_q:
:return:
"""
DEVNULL = open(os.devnull, 'w')
while True:

ip = job_q.get()

if ip is None:
break

try:
subprocess.check_call(['ping', '-c1', ip],
stdout=DEVNULL)
results_q.put(ip)
except:
pass
os.close(os.devnull)


def get_my_ip():
"""
Find my IP address
:return:
"""
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
ip = s.getsockname()[0]
s.close()
print("Host IP address is "+ip+"\n")
return ip


def map_network(pool_size=255):
"""
Maps the network
:param pool_size: amount of parallel ping processes
:return: list of valid ip addresses
"""

ip_list = list()

# get my IP and compose a base like 192.168.1.xxx
ip_parts = get_my_ip().split('.')
base_ip = ip_parts[0] + '.' + ip_parts[1] + '.' + ip_parts[2] + '.'

# prepare the jobs queue
jobs = multiprocessing.Queue()
results = multiprocessing.Queue()

pool = [multiprocessing.Process(target=pinger, args=(jobs, results)) for i in range(pool_size)]

for p in pool:
p.start()

# cue hte ping processes
for i in range(1, 255):
jobs.put(base_ip + '{0}'.format(i))

for p in pool:
jobs.put(None)

for p in pool:
p.join()

# collect he results
while not results.empty():
ip = results.get()
ip_list.append(ip)

return ip_list


if __name__ == '__main__':

print('Mapping...')
lst = map_network()


#Define your Friendly IP here

friendly_ip = ['192.168.43.21']
for i in lst:
flag = 0
for j in friendly_ip:
if i == j:
pass
else:
flag = 1
if(flag==1):
print("Enemy IP is :"+i)



print("All IP Address connected to network is : "+str(lst))

P.S 这不是 OSError [Errno 24] 的任何重复问题我都试过了但没有成功

最佳答案

看起来这里可能会发生一些事情。我无法重现您的确切错误,这是有道理的,因为它可能与您操作系统的文件限制有关。

  1. 您有 os.close(os.devnull),我很确定它应该是 DEVNULL.close()。这可能意味着您永远不会关闭文件描述符,从而导致“打开的文件过多”

  2. 您可以尝试以较少的并发运行(较小的 pool_size)。这可能会使您的程序变慢,但它至少可以工作。我将它设置为 2,虽然花了一段时间,但它最终还是完成了。

关于python - 操作系统错误 : [Errno 24] Too many open files: '/dev/null' or No IP is showing on my connected network,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55277759/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com