相关疑难解决方法(0)

968
推荐指数
27
解决办法
19万
查看次数

从current.futures使用ThreadPoolExecutor时,max_workers的数量是多少?

在决定从current.futures在ThreadPoolExecutor中将max_workers设置为什么时,应考虑哪些因素?

只要您可以期望Python 3.5+可用,是否有任何理由不将max_workers设置为None,然后将“默认设置为计算机上的处理器数量乘以5”,如此处的文档所述? https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.ThreadPoolExecutor

python python-3.x concurrent.futures

13
推荐指数
1
解决办法
4054
查看次数

使用Python扫描主机中的每个可能端口

我正在编写一个程序,需要扫描主机中的所有65535个端口,搜索那些打开的端口.这是我到目前为止,它的工作原理,但每次执行脚本时都会产生不同的结果,为什么会这样?

def check_open_port(host, port):
    s = socket.socket()
    s.settimeout(0.1)
    # the SO_REUSEADDR flag tells the kernel to reuse a local 
    # socket in TIME_WAIT state, without waiting for its natural
    # timeout to expire.
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

    try:
        code = s.connect_ex((host, port))
        s.close()

        if code == 0:
            return True
        else:
            return False
    except socket.error:
        return False


def get_open_ports(host, max_port=65535):
    open_ports = []

    def worker(port):
        if check_open_port(host, port):
            open_ports.append(port)


    pool = ThreadPoolExecutor(max_workers=10000)
    [pool.submit(worker, port) for port in range(1, max_port + …
Run Code Online (Sandbox Code Playgroud)

python ip port tcp python-multithreading

5
推荐指数
2
解决办法
1323
查看次数