且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python套接字无法正确关闭连接

更新时间:2022-05-06 08:21:27

您正在体验已连接套接字的 TIME_WAIT 状态.即使您已关闭插槽,它仍然会持续挥霍几分钟. UNIX指南套接字常见问题解答.

You are experiencing the TIME_WAIT state of connected sockets. Even though you've closed your socket, it still has lingering consequences for a couple minutes. The reasons for this, as well as a socket flag you can set to disable the behavior (SO_REUSEADDR), are explained in the UNIX guide socket FAQ.

简而言之,

server = socket.socket()
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.bind(...)
...