#! /Usr/bin/ENV Python #-*-encoding: UTF-8-*-import socketimport selecthost = "" Port = 50000 S = socket. socket (socket. af_inet, socket. sock_stream) s. BIND (host, Port) s. listen (5) print "begin... "While 1: infds, outfds, errfds = select. select ([s,], [], [], 5) # If the infds status changes, process it. Otherwise, if Len (infds) is ignored )! = 0: clientsock, clientaddr = S. accept () infds_c, outfds_c, errfds_c = select. select ([clientsock,], [], [], 3) If Len (infds_c )! = 0: Buf = clientsock. Recv (8196) If Len (BUF )! = 0: Print (BUF) clientsock. Close () print "clientsock closed" print "no data coming"
An article on zookeeperArticleDescribes the non-blocking Socket Type of Python.
In the original generation of primary keys, a new SELECT statement is added and two select statements are used,
The first select has many clients to connect. If there is no client to connect after five seconds, it shows "no data coming"
The second select is to directly close the client if no message is sent after 3 seconds, which will not cause blocking.