简体   繁体   English

显示所有连接到套接字服务器的客户端并向他们发送数据

[英]Show all the clients connected to a socket server and send them data

I have this simple code: 我有这个简单的代码:

import socket

socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.bind((host, port))
socket.listen()
while True:
    client_socket, addr = socket.accept()
    send = input("Send: ") # but I need a way to send it to all the clients connected
    if send == "devices":
    # here I'd have a list of all devices connected
    client_socket.send(send.encode())
    data = client_socket.recv(4096)
    print (data)

As I wrote in the comments, I need a way to manage them all in one. 正如我在评论中所写,我需要一种将其全部管理的方法。 How can I do? 我能怎么做? Maybe with _thread library? 也许与_thread库?

You could mainitain a list of clients that can be passed to an external function that performs an action on all clients. 您可以维护一个客户端列表,该列表可以传递给对所有客户端执行操作的外部函数。

import socket

host = ''
port = 1000
max_connections = 5


socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.bind((host, port))
socket.listen(max_connections)
clients = []    # Maintain a list of clients
try:
    while True:
        client_socket, addr = socket.accept()
        clients.append(client_socket)    #Add client to list on connection
        i_manage_clients(clients)       #Call external function whenever necessary
except KeyboardInterrupt:
    socket.close()

def i_manage_clients(clients):    #Function to manage clients
    for client in clients:
        client.send('Message to pass')

The above example demonstrates how send data to all clients at once. 上面的示例演示了如何一次将数据发送到所有客户端。 You could use the 您可以使用

import socket
from thread import *

host = ''
port = 1000
max_connections = 5


socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.bind((host, port))
socket.listen(max_connections)
try:
    while True:
        client_socket, addr = socket.accept()
        start_new_thread(i_manage_client, (client_socket,addr))
except KeyboardInterrupt:
    socket.close()

def i_manage_client(client_socket, addr):    #Function to manage clients
    client_socket.send('Message to pass')
    data = client_socket.recv(4096)
    print(client_socket)
    print(addr)
    print(data)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 以间隔向所有连接的客户端发送数据。 烧瓶插座 - Send data to all connected clients with interval. Flask-socketio Socket.sendall()未发送到所有连接的客户端 - Socket.sendall() not sending to all connected clients 如何将 socket.io 挂载到 fastapi 应用程序并向所有连接的客户端发送广播 - How to mount socket.io to fastapi app and send broadcast to all connected clients 发送xmpp消息到所有连接的客户端/资源 - Send an xmpp message to all connected clients/resources 保存已连接的套接字,以后再向他们发送消息 - Saving a socket that connected to later send them a message 如何从服务器发送请求断开连接到所有客户端和服务器仍然存在(python套接字) - How to send request disconnect from server to all clients and server still live (python socket) Python 3:套接字服务器使用sendto()函数发送到多个客户端 - Python 3: Socket server send to multiple clients with sendto() function 具有多个客户端的套接字服务器 - Socket Server with multiply Clients 多个客户端连接到Tornado Server - Multiple clients connected to Tornado Server Python - 不允许发送或接收数据的请求,因为套接字未连接 - Python - A request to send or receive data was disallowed because the socket is not connected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM