Python's multiple concurrent sockets

Source: Internet
Author: User

Import socketserver# Socketserver There are 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 put Socke encapsulation, add some methods, there is no implementation of multiple concurrency and multithreading #这些方法不适用每个请求都耗费长时间才能完成 # Socketserver. tcpserver# Socketserver. udpserver# Socketserver. unixstreamserver# Socketserver. unixdatagramserver# Socketserver. forkingmixin# Socketserver. threadingmixin# ================================================================================================ ========# we really want to use the following 4 ways # # Multi-process socketserver# socketserver. forkingtcpserver# Socketserver. forkingudpserver# Multi-process socketserver# socketserver. threadingtcpserver# Socketserver. threadingudpserver# ============================================================================================ ============ #这里我们写一个socketserver # 1, define a class Mytcphandler (Socketserver. Baserequesthandler): def handle (self): #所有和客户端交互的动作全部要在这里写, this is blank by default and must be written handle this guard print ("New conn:", self . client_address) data = SELF.REQUEST.RECV (1024x768) print ("Client data:", str (data,encoding= "Utf-8")) Self . REquest.send (data) if __name__ = = ' __main__ ': Ip_bind = ("127.0.0.1", 9000) server = Socketserver.  Threadingtcpserver (Ip_bind,mytcphandler) #每次来一个连接, build an instance server.serve_forever () #让这个server一直运行 # The client's writing is the same as a single process

  

Python's multiple concurrent sockets

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.