【发布时间】:2020-06-17 17:21:28
【问题描述】:
我正在尝试使用多线程为多个套接字建立连接
这是代码
import socket as sc
if __name__ == '__main__':
#setup()
ports = [10000, 10010, 10020, 10030]
init_sockets()
init_threads()
def init_sockets():
global host_ip
global sockets
host_ip = sc.gethostname()
sockets = []
for port in ports:
socket = sc.socket()
socket.bind((host_ip, port))
socket.listen()
sockets.append(socket)
def init_threads():
threads = [
threading.Thread(target= init_connection, args= [socket])
for socket in sockets
]
for thread in threads:
thread.start()
def init_connection(socket):
client, address = socket.accept()
运行代码时出现此错误
ConnectionAbortedError: [Errno 53] Software caused connection abort
函数init_threads()中的thread.start()语句出现错误
我不知道为什么会这样,非常感谢任何帮助。我正在尝试并行运行多个套接字连接,如果这样不可能,我愿意接受建议
【问题讨论】:
-
代码似乎没问题。我的主机名有问题,但将其更改为 127.0.0.1 解决了它。确保其中一个端口未被其他应用程序使用,
标签: python multithreading sockets python-multithreading python-sockets