Python network programming using Select to implement the socket full duplex asynchronous communication function

Source: Internet
Author: User
This article mainly introduces the Python network programming using Select to implement the socket full-duplex asynchronous communication function, here to share to everyone, the need for friends can refer to the next

The examples in this paper describe Python network programming using Select to implement the socket full duplex asynchronous communication function. Share to everyone for your reference, as follows:

In the previous "simple usage of TCP sockets for network programming in Python", we have implemented TCP client-server communication, but the function is very limited, sending and receiving messages cannot be done at the same time.

Next I will use Select this module, to achieve full-duplex communication (at any time can receive information and send information), of course, with multithreading can also be done, this is something.

So, what is a select?

Select-Manage multiple socket connections in a single-threaded network Service medium program

The prototype for select is (Rlist,wlist,xlist[,timeout]), where rlist is the object waiting to be read, Wlist is the object waiting to be written, xlist is the object waiting for the exception, and the last is an optional object, specifying the time to wait, in S.

select()The return value of the method is a ternary group of ready-to-use objects, and if no object is ready in the timeout period, the return value will be an empty list.

It takes the form of polling to implement asynchronous communication.

In the following program, currently mainly supports 1 to 1 communication, when either party sends the string ' 88 ', indicates the end of the communication.

Let's take a look at the specific implementation:

The first is the server.

#!/usr/bin/python ' Test TCP server ' from socket import *from time import ctimeimport selectimport syshost = ' PORT = 21567BU Fsiz = 1024ADDR = (HOST, PORT) Tcpsersock = socket (af_inet, Sock_stream) tcpsersock.bind (ADDR) Tcpsersock.listen (5) input = [Tcpsersock, Sys.stdin] #input是一个列表, initial welcome socket and standard input while True:print ' Waiting for connection ... ' tcpclisock, addr = tcp Sersock.accept () print ' ... connected from: ', addr input.append (tcpclisock) #将服务套接字加入到input列表中 while True:readyinput , readyoutput,readyexception = Select.select (input,[],[]) #从input中选择, taking turns processing the client's request connection (Tcpsersock), the message sent by the client ( Tcpclisock), and server-side send Message (stdin) for indata in readyinput:if indata==tcpclisock: #处理client发送来的消息 data = TcpC       LISOCK.RECV (bufsiz) print data if data== ': Input.remove (Tcpclisock) Break else: #处理服务器端的发送消息 data = raw_input (' > ') if data== ': Tcpclisock.send ('%s '% (data)) INP   Ut.remove (Tcpclisock) break     Tcpclisock.send (' [%s]%s '% (CTime (), data)) if data== ' in ': Break Tcpclisock.close () Tcpsersock.close () 

The following is the client code, which is very similar, except that there is no need to process the request information compared to the server.

#!/usr/bin/python ' Test TCP client ' from socket import *from time import ctimeimport selectimport syshost = ' localhost ' PORT = 21567BUFSIZ = 1024ADDR = (HOST, PORT) Tcpclisock = socket (af_inet, Sock_stream) tcpclisock.connect (ADDR) input = [Tcpcliso Ck,sys.stdin]while True:  readyinput,readyoutput,readyexception = Select.select (input,[],[]) for  Indata in Readyinput:    If indata==tcpclisock:      data = TCPCLISOCK.RECV (bufsiz)      print data      if data== ' in ':        Break    Else:      data = raw_input (' > ')      if data== ' ":        tcpclisock.send ('%s '% (data))        break      tcpclisock.send (' [%s]%s '% (CTime (), data))  If data== ' ":    breaktcpclisock.close ()

So far, one-to-one full-duplex chat communication has been implemented.

The following of course to think about, how to achieve multi-to-many communication?

We know that a server can serve multiple clients, that is, the server and the client itself has a one-to-many relationship, then we can use the server as a middleman to communicate information, so as to achieve multi-to-many communication?

For example, a wants to communicate with B, then a sends the information to the server and forwards it to B via the server. According to this idea to write, the completion of many-to-many communication should be possible, interested can try.

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.