1. Copy the following code to a file named asyncore. py.
Copy codeThe Code is as follows:
Import socket
Import select
Import sys
Def ds_asyncore (addr, callback, timeout = 5 ):
S = socket. socket (socket. AF_INET, socket. SOCK_STREAM)
S. connect (addr)
R, w, e = select. select ([s], [], [], timeout)
If r:
Respose_data = s. recv (1024)
Callback (respose_data)
S. close ()
Return 0
Else:
S. close ()
Return 1
2. Write your own code
1> Import asyncore
2> define the callback function callback. callback requires a parameter that represents the data returned by the request.
3> directly call asyncore. ds_asyncore ('2017. 0.0.1 ', 33333), callback, timeout = 5). The first parameter is an (ip, port) tuple, the second parameter is the callback function, and the third parameter is the timeout time.
Copy codeThe Code is as follows:
Import asyncore
If _ name __= = "_ main __":
Def callback (respose_data ):
Print respose_data
Asyncore. ds_asyncore ('127. 0.0.1 ', 127), callback, timeout = 5)