1 server-side code2 3 ImportSocketserver4 classMytcphandler (socketserver.baserequesthandler):5 """6 The RequestHandler class for socket server.7 It is instantiated once per connection being established (request from client) to the Socker server.8 And must is override the handler () method to implement communication to the client.9 """TenConnection_account =0 One defHandle (self):#Redefine method ' Handl () ' in parent class AMytcphandler.connection_account + = 1 - Print("The %d client connected!"% Mytcphandler.connection_account)#How to statistic Total No. - #Self.request is the TCP socket of the connected to the client the while1: -Self.data = SELF.REQUEST.RECV (1024). Strip () - #If connected client dead, exit loop - if notSelf.data: + Break - Print("Client:%s \ n" + "Send:%s") %(Self.client_address[0],self.data) ASelf.request.sendall ("GOT IT!") at - if __name__=="__main__": -Host,port ="localhost", 8888 - #creating the socket server and binding to HOST::P ort = = "localhost":: 8888 -Server =Socketserver.threadingtcpserver ((host,port), Mytcphandler) - #keeping server running until interrupt (Ctrl + C) inServer.serve_forever (poll_interval=0.5)
client-side code with single thread
Reference Blog, http://www.cnblogs.com/zzyzz/p/5581503.html
Python socket (multi-threaded)