Python Select sample

Source: Internet
Author: User

ImportSelectImportSocketImportSYSImportQueue#Create A TCP/IPServer =Socket.socket (socket.af_inet, socket. SOCK_STREAM) server.setblocking (0)#Bind the socket to the portServer_address = ('localhost', 10000)Print(Sys.stderr,'starting up on%s port%s'%server_address) Server.bind (server_address)#Listen for Incoming connectionsServer.listen (5)#Sockets from which we expect to readInputs =[Server]#Sockets to which we expect to writeoutputs = [ ]#Outgoing message queues (socket:queue)Message_queues = {} whileInputs:#Wait for at least one of the sockets to is ready for processing    Print(Sys.stderr,'waiting for the next event') readable, writable, exceptional=Select.select (inputs, outputs, inputs)#Handle Inputs     forSinchReadable:ifS isServer:#a "readable" socket is ready to accept A connectionConnection, client_address =s.accept ()Print(Sys.stderr,'Connection from', client_address) connection.setblocking (0) inputs.append (connection)#Give The connection a queue for data we want to sendMessage_queues[connection] =queue. Queue ()Else: Data= S.RECV (1024)            ifData:#A readable client socket has data                Print(Sys.stderr,'received '%s ' from%s'%(data, S.getpeername ())) Message_queues[s].put (data)#ADD output channel for response                ifS not inchOutputs:outputs.append (s)Else:                #interpret empty result as closed connection                Print(Sys.stderr,'closing', client_address)#Stop listening for input on the connection                ifSinchOutputs:outputs.remove (s) inputs.remove (s) s.close () #Remove Message Queue                delMessage_queues[s]#Handle outputs     forSinchWritable:Try: Next_msg=message_queues[s].get_nowait ()exceptqueue. Empty:#No messages waiting so stop checking for writability.            Print(Sys.stderr,'  ', S.getpeername (),'Queue Empty') Outputs.remove (s)Else:            Print(Sys.stderr,'sending "%s" to%s'%(Next_msg, S.getpeername ())) S.send (next_msg)#Handle "Exceptional conditions"     forSinchExceptional:Print(Sys.stderr,'exception Condition on', S.getpeername ())#Stop listening for input on the connectionInputs.remove (s)ifSinchOutputs:outputs.remove (s) s.close ()#Remove Message Queue        delMessage_queues[s]

ImportSocketImportsysmessages= ['This is the message.',             'It 'll be sent',             'In parts.',]server_address= ('localhost', 10000)#Create ATCP/IP SocketSocks=[Socket.socket (socket.af_inet, socket. SOCK_STREAM), Socket.socket (Socket.af_inet,socket. SOCK_STREAM),]#Connect Thesocket to the port where the server is listeningPrint(Sys.stderr,'connecting to%s port%s'%server_address) forSinchSocks:s.connect (server_address) forMessageinchmessages:#Send messages on both sockets     forSinchSocks:Print(Sys.stderr,'%s:sending "%s"'%( s.getsockname (), message)) S.send (bytes (message,encoding='Utf-8'))    #Read responses on both sockets     forSinchSocks:data= S.RECV (1024)        Print(Sys.stderr,'%s:received "%s"'%(S.getsockname (), data))if  notData:Print(Sys.stderr,'Closingsocket', S.getsockname ()) S.close ()

Python Select sample

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.