Python implements single-thread multi-task non-blocking TCP server, python single-Thread

Source: Internet
Author: User

Python implements single-thread multi-task non-blocking TCP server, python single-Thread

The examples in this article share the code for implementing Single-thread multi-task non-blocking TCP server in python for your reference. The details are as follows:

# Coding: utf-8from socket import * #1. create server socketsock = socket (AF_INET, SOCK_STREAM) #2. bind host and port addr = ('', 7788) # sock. bind (addr) #3. set the maximum number of listeners and concurrent sock. listen (10) #4. set to non-blocking sock. setblocking (False) # Save the client socketclientAddrList = [] # print (sock .) while 1: try: clientSocket, clientAddr = sock. accept () handle T: pass else: print ("arrival of a new client: % s" % str (clientAddr) clientSocket. setblocking (False) clientAddrList. append (clientSocket, clientAddr) for clientSocket, clientAddr in clientAddrList: try: recvData = clientSocket. recv (1024) failed T: pass else: if len (recvData)> 0: print ("% s: % s" % (str (clientAddr), recvData) else: clientSocket. close () clientAddrList. remove (clientSocket, clientAddr) print ("% s offline" % str (clientAddr ))

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.