我想要一个功能来检查数据是否在套接字中等待读取,然后再读取它。这样的事情会有所帮助:
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)
关于python - 检查python套接字中的数据是否可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23355710/