python - Python 中的最大 TCP 连接数?

标签 python tcp connection

我在本地主机上使用 Python 工作。我写了一个原始服务器只读取 TCP 套接字,比如在端口 50001 中。

然后我尝试了最大客户端连接数:

def rawMultiConn(threadnum = 10000):
    g_event = threading.Event()
    def threadfn():
        sockets = [socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                   for i in range(threadnum)]
        for s in sockets:
            s.connect(('localhost', SERVER_PORT))
        g_event.wait()
        for s in sockets: s.close()

    t = threading.Thread(target = threadfn)
    t.start()
    g_event.set()
    t.join()

但是大约3000次连接后,出现异常:

[Errno 10055] An operation on a socket could not be performed because the system lacked sufficient buffer space or because a queue was full

那么我该如何解决它并最大化连接呢?

最佳答案

检查盒子和运行脚本的用户的 ulimitquota/etc/security/limits.conf 也可能包含您可能想要修改的资源限制。

尝试运行 ulimit -s unlimited

此外,ulimit -n 将显示允许打开的文件描述符/套接字的最大数量。这可能也需要修改。


一些手册页和引用链接:

关于python - Python 中的最大 TCP 连接数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7748898/

相关文章:

Python for 循环以步长 3 迭代

python - 收到 win32ui.error : CreateFile when trying to save a screenshot with python and winapi

python - 访问不同 block 的 Jinja 宏

java - XML自动编辑器(基于XSD方案)

go - 读取 net.Conn 缓冲区的第一个 UVarInt

ssl - 无法建立 SSL 连接

scala - 使用 Akka IO 通过 Tcp 客户端发送非 ByteString

c# - TcpClient.NoDelay 会影响已经写入的数据吗?

RSelenium rsDriver 对等 SSL 证书问题

PHP:MySQL 如果我们不关闭打开的连接怎么办