接受多个客户端的服务器

时间:2014-07-17 12:42:00

标签: sockets python-3.x tcp asyncsocket

大家好我已经在python中使用tcp socket创建了一个聊天,但是服务器只接受了我试图搜索的一个客户端,但是我很难理解如何创建一个接受多个客户端的服务器......任何人都可以帮助我吗?感谢

1 个答案:

答案 0 :(得分:0)

您可以查看this。基本上,它应该是这样的(注意样品处理只是一个例子,你将不得不做更好的事情):

import threading                                                                                                                                                      
import socket   

def print_message(socket):                                                                                                                                            
  data = socket.recv(512)                                                                                                                                             
  print(data)                                                                                                                                                         

def handle_client(clientsocket):                                                                                                                                      
  print("We have a new client")                                                                                                                                         
  thread = threading.Thread(target=print_message, args=(clientsocket,))                                                                                                 
  thread.run()


# create an INET, STREAMing socket                                                                                                                                    
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)                                                                                                      
# bind the socket to a public host, and a well-known port                                                                                                             
serversocket.bind((socket.gethostname(), 8080))                                                                                                                       
# become a server socket                                                                                                                                              
serversocket.listen(5)                                                                                                                                                

while True:                                                                                                                                                           
  # accept connections from outside                                                                                                                                   
  (clientsocket, address) = serversocket.accept()                                                                                                                     
  # now do something with the clientsocket                                                                                                                            
  # in this case, we'll pretend this is a threaded server                                                                                                             
  ct = handle_client(clientsocket)