如何修改python套接字服务器代码以适应两个客户端?

如何修改python套接字服务器代码以适应两个客户端?,python,multithreading,sockets,server,communication,Python,Multithreading,Sockets,Server,Communication,我正在尝试将数据从我的客户机_1发送到客户机-2。此传输必须通过服务器程序进行。从客户端_1到服务器的数据传输成功,但服务器无法连接到客户端_2。请注意,对于我的应用程序**,服务器的作用类似于被动元素,而不是主动元素**,即服务器没有主动发送或广播任何数据。它就像客户端1和客户端2之间的桥梁 我的问题是如何修改我的服务器代码,以便我能够将我的两个客户端与我的服务器连接起来,并且应该能够将服务器中接收到的数据传输到客户端2 下面我正在附加我的服务器代码:: import socket #from

我正在尝试将数据从我的客户机_1发送到客户机-2。此传输必须通过服务器程序进行。从客户端_1到服务器的数据传输成功,但服务器无法连接到客户端_2。请注意,对于我的应用程序**,服务器的作用类似于被动元素,而不是主动元素**,即服务器没有主动发送或广播任何数据。它就像客户端1和客户端2之间的桥梁

我的问题是如何修改我的服务器代码,以便我能够将我的两个客户端与我的服务器连接起来,并且应该能够将服务器中接收到的数据传输到客户端2

下面我正在附加我的服务器代码::

import socket
#from threading import Thread

def server_program():

    all_connections = []
    all_address = []

    host = socket.gethostname()       ###### get the hostname ###########
    port = 5000                   ##################### initiate port no above 1024 ###############
    server_socket = socket.socket()                 ############# initiate the connection ##########e

    server_socket.bind((host, port))     ############### bind host address and port together ##############

            ############### configure how many client the server can listen simultaneously ################
    server_socket.listen(5)


    #def accepting_connections():
     #   for c in all_connections:
      #      c.close()

    #del all_connections[:]
    #del all_address[:]


    conn, address = server_socket.accept() 
   # socket.setblocking(1)
    all_connections.append(conn)
    all_address.append(address)
    print("Connection from: " + str(address))
    while True:
                                     ################ receive data stream. it won't accept data packet greater than 1024 bytes ###########
        data = conn.recv(1024).decode()
        if not data:
                                        ########### if data is not received break #############
             break
        print("from housekeeping unit: " + str(data))
                # data = input(' -> ')
        conn.send(data.encode())  ########## send data to the client ###########

    conn.close() ############## close the connection ###############


if __name__ == '__main__':
    server_program()

请参阅多处理模块()

尝试使用
选择
模块。这将允许您接受客户端套接字,并保持服务器打开以侦听更多连接。