我想要一个功能来检查数据是否在读取之前在套接字中等待读取.这样的事情会有所帮助:
if (data available) then read data
else wait in blocking mode till data becomes available
我怎样才能在Python中实现这一点
最佳答案
while 1:
socket_list = [sys.stdin,s]
# Get the list sockets which are readable
read_sockets,write_sockets,error_sockets = select.select(socket_list,[],[])
for sock in read_sockets:
#incoming message from remote server
if sock == s:
data = sock.recv(4096)
if not data :
print '\nDisconnected from server'
sys.exit()
else :
#print data
sys.stdout.write(data)
#user entered a message
else :
msg = sys.stdin.readline()
s.send(msg)