Python uses select for asynchronous communication 1

Source: Internet
Author: User

#-*-Coding:utf-8-*-

# python:2.x

__author__ = ' Administrator '

# function: A notification that waits for input or the output channel is ready

"""

Allows access to specific platform I/O monitoring functions, the most portable interface is the POSIX function Select ()

unix and windwods " Span style= "font-family: the song Body;" > this 2 A function, this module can implement asynchronous communication, select poll () and select (), select () The prototype is (Rlist,wlist,xlist[,timeout])

Rlist waits for the object to be read,wlist waits for the final object , xlist waits for the exception object, the last optional object, specifies the wait time, the unit is s, The Select () method returns the value of the prepared object is ternary, if No object is ready in timeout time, then the return value is empty list

"""

#select Server

From socket Import *

Server=socket (Af_inet,sock_stream)

Server.setsockopt (sol_socket,so_reuseaddr,1) #socket. setsockopt (level, optname, value)

#Set the value of the given socket option (see the Unix manual page setsockopt (2)).

# The needed symbolic constants is defined in the socket module (so_* etc). The value can be a integer or a string representing a buffer.

# in the latter case it's up to

# The caller to ensure this string contains the proper bits (see the optional built-in module

# struct for a-to-encode C structures as strings).

Server.bind ((' ', 10000))

Inputs=[server]

From select Import *

While True:

Re1,ws,es=select (inputs,[],[])

For R in Re1:

If r in server:

Clientsock,clientaddr=r.accept ()

Inputs.append (Clientsock)

Else

DATA=R.RECV (1024)

If not data:

Inputs.remove (R)

Else

Print data

#-*-Coding:utf-8-*-

# python:2.x

__author__ = ' Administrator '

# Client

From socket Import *

Host= ' 127.0.0.1 '

port=10000

S=socket (Af_inet,sock_stream)

S.connect ((Host,port))

S.send (' Hello world! ')

S.close ()

Python uses select for asynchronous communication 1

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.