Python Network programming Note two

Source: Internet
Author: User

Using the Select module for IO multiplexing service side

1 ImportSocket2 ImportSelect3 #only Select.select is supported on Windows, poll Epoll is not supported4 5HOST ="127.0.0.1"6PORT = 99997Buffsize = 10248ADDR =(HOST, PORT)9 #creating server-side listener socketsTenTcpsocket =Socket.socket (socket.af_inet, socket. SOCK_STREAM) One Tcpsocket.bind (ADDR) ATcpsocket.listen (5) - tcpsocket.setblocking (0) -  the  - #listening for read events -Inputs =[Tcpsocket] - #listening to write events +outputs = [] - #Monitoring Exception Events +exceptions = [] A  at  whileTrue: -     #whenever there is a client connection, the returned r_list contains the listener's server socket Tcpsocket -R_list, w_list, e_list = select.select (inputs, outputs, exceptions, 1) -     Print(len (inputs)) -      forRinchr_list: -         ifr = =Tcpsocket: inRequest, addr =r.accept () - request.setblocking (0) to             #Add the client socket to the listener list, and the next time the client sends the data, the socket can be included in the R_list . + inputs.append (Request) -         #If the socket is not equal to tcpsocket, it means that the client sends the data the         Else: *             Try: $Databytes = R.RECV (1024)Panax NotoginsengDatastr = str (databytes, encoding="Utf-8") -                 Print("received from", R.getpeername ()," : ", Datastr) theR.sendall (Bytes ("you have received your e-mail"+ Datastr, encoding="Utf-8")) +             #Description Client Disconnects A             #Connectionreseterror the             exceptException as E: +                 #remove the client socket from the listener list - Inputs.remove (R) $ r.close () $Tcpsocket.close ()

Python Network programming Note two

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.