我想要一个功能来检查数据是否在读取之前在套接字中等待读取。这样的事情会有所帮助:
if (data available) then read data
else wait in blocking mode till data becomes available
我如何在Python中实现这一目标
答案 0 :(得分:3)
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)