0
我有一个python套接字服务器正在运行,还有套接字客户端。识别Python中的线程
现在,例如说有3个客户端连接到同一台服务器。请在下面找到服务器的代码。
#!/usr/bin/python # This is server.py file
import socket # Import socket module
import threading
serversocket = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
port = 1234 # Reserve a port for your service.
serversocket.bind((host, port)) # Bind to the port
serversocket.listen(5)
print("Bound the port ",port,"on Machine : ",host,", and ready to accept connections.\n")
def clientThread(connection):
while True:
data=connection.recv(1024)
if not data:
break
connection.send("Thanks")
connection.close()
def sendMessage(connection, message):
connection.send(message)
while 1:
connection, address = serversocket.accept()
start_new_thread(clientthread, (connection,))
serversocket.close();
现在,我需要调用的sendMessage特定客户,说出去的客户A,B和C,其发送到B.在这种情况下,如何识别线程并调用该函数?
您也可以从单线程聊天服务器获得一些想法:http://www.binarytides.com/code-chat-application-server-client-sockets-python/ – ErikR