1 Copy the following code to a file named asyncore.py
Copy Code code 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> defines a callback function Callback,callback requires a parameter that represents the request return data
3> directly calls Asyncore.ds_asyncore (' 127.0.0.1 ', 33333), callback,timeout=5), where the first argument is a (ip,port) tuple, the second is a callback function, and the third is the timeout period.
Copy Code code as follows:
Import Asyncore
If __name__== "__main__":
DEF callback (Respose_data):
Print Respose_data
Asyncore.ds_asyncore ((' 127.0.0.1 ', 33333), callback,timeout=5)