In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
The knowledge of this article "Python how to use select to achieve socket full-duplex asynchronous communication function" is not understood by most people, so the editor summarizes the following content to you, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "Python how to use select to achieve socket full-duplex asynchronous communication" article.
Select-manage multiple socket connections in a single-threaded network service server program
The prototype of 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 exception, and the last one is optional, which specifies the waiting time in s.
The return value of the select () method is the triple of the prepared object, and if no object is ready within the timeout time, the return value will be an empty list.
It uses the way of polling to achieve asynchronous communication.
In the following program, 1-to-1 communication is mainly supported, and when either party sends the string '88', it ends the communication.
Let's take a look at the implementation:
The first is the server.
#! / usr/bin/python'test TCP server'from socket import * from timeimport ctimeimport selectimport sysHOST =''PORT = 21567BUFSIZ = 1024ADDR = (HOST, PORT) tcpSerSock = socket (AF_INET, SOCK_STREAM) tcpSerSock.bind (ADDR) tcpSerSock.listen (5) input = [tcpSerSock, sys.stdin] # input is a list, initially with a welcome socket and standard input while True: print' waiting for connection...' TcpCliSock, addr = tcpSerSock.accept () print '... connected from:',addr input.append (tcpCliSock) # add service sockets to the input list while True: readyInput,readyOutput,readyException = select.select (input, []) # Select from input to take turns processing client request connections (tcpSerSock) and messages from client (tcpCliSock) And server-side stdin for indata in readyInput: if indata==tcpCliSock: # processing messages sent by client data= tcpCliSock.recv (BUFSIZ) print data if data=='88': input.remove (tcpCliSock) break else: # processing server-side send messages data= raw_input ('>') if Data=='88': tcpCliSock.send ('% s'% (data)) input.remove (tcpCliSock) break tcpCliSock.send ('[% s]% s'%) (ctime () Data)) if data=='88': break tcpCliSock.close () tcpSerSock.close ()
The following is the code for the client, 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 timeimport ctimeimport selectimport sysHOST = 'localhost'PORT = 21567BUFSIZ = 1024ADDR = (HOST, PORT) tcpCliSock = socket (AF_INET, SOCK_STREAM) tcpCliSock.connect (ADDR) input = [tcpCliSock,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=='88': break else: data= raw_input ('>') if data=='88': tcpCliSock.send ('% s'% (data)) break tcpCliSock.send ('[% s]% s'% ctime () Data)) if data=='88': breaktcpCliSock.close () the above is the content of the article on "how Python uses select to implement socket full-duplex asynchronous communication" I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.