简体   繁体   中英

Python Socket: Storing 'connections'?

I have multi-threaded python socket server:

connection, client_address = sock.accept()

And i need to store 'connection' to use for other thread.

How to do that?

Perhaps a list?

connections = []
connection, client_address = sock.accept()
connections.append(connection)

connection[0]  # this is your connection

right, when you use s.accept(), it creates like a subchannel you can talk to that client privatly in. so, if you do this: connections.append(connection) , you can then select the one in the list you want and go connections[i].send(str.encode("xyz")) or, if you just want to send a message to every client, just use sock.send("xyz") , which will send it to everyone. hope this is helpfull :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM