This article was reproduced from: http://www.cnblogs.com/bainianminguo/p/7337210.html
First of all, look at the socket multi-concurrency server code, here is a multi-threaded implementation of multiple concurrency Socketserver
ImportSocketserver#Socketserver has four basic classes, the latter two are not commonly used, these 4 classes handle concurrent requests are synchronous, they are not multi-threaded,#They just encapsulated the Socke, added some methods, and there's not much concurrency and multithreading .#These methods do not apply to each request that takes a long time to complete#Socketserver. TCPServer#Socketserver. Udpserver#Socketserver. Unixstreamserver#Socketserver. Unixdatagramserver#Socketserver. Forkingmixin#Socketserver. ThreadingMixIn# ========================================================================================================#We're really going to use the following 4 methods#Socketserver of multiple processes#Socketserver. Forkingtcpserver#Socketserver. Forkingudpserver#Socketserver of multiple processes#Socketserver. Threadingtcpserver#Socketserver. Threadingudpserver# ========================================================================================================classMytcpserverclass (socketserver. Baserequesthandler):defhandle (self):Print("waiting for new connect:") Print("New client:", self.client_address) whileTrue:Try: Data=SELF.REQUEST.RECV (1024) Print("The client:", Self.client_address,str (data,encoding='Utf-8')) exceptException:Print("The client [%s] is unconnected!"% (self.client_address[1])) Self.request.close () Breakif __name__=='__main__': Ipbind=('127.0.0.1', 2345) Server=Socketserver. Threadingtcpserver (Ipbind,mytcpserverclass) server.serve_forever ()
Then look at the client's code:
ImportSocketipbind=('127.0.0.1', 2345) C=Socket.socket () c.connect (ipbind) whileTrue:c_input=input ("Client:") c.send (bytes (c_input,encoding='Utf-8')) S_send=C.RECV (1024) Print("Service side:", str (s_send,encoding='Utf-8'))
Python's multiple concurrent Sockets (ZZ)