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