gpt4 book ai didi

python - 检查python套接字中的数据是否可用

转载 作者:太空狗 更新时间:2023-10-30 00:11:06 26 4
gpt4 key购买 nike

我想要一个功能来检查数据是否在套接字中等待读取,然后再读取它。这样的事情会有所帮助:

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com