Python's multiple concurrent Sockets (ZZ)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.