gpt4 book ai didi

python - if 语句被跳过,即使语句为真

转载 作者:太空宇宙 更新时间:2023-11-04 10:15:19 25 4
gpt4 key购买 nike

我刚开始在 python 上进行 TCP 服务器和客户端编程。然而,其中一个 if 语句不起作用并且没有任何意义:

while (1):
data = s.recv(1024)
if data == 'finish':
break
print data
print 'Finish'

有时最后一条 channel 会打印“完成”而不是“完成”,并且无法退出循环。这应该永远不会发生,因为如果 data='finish','print data' 语句将被跳过。谁能告诉我为什么会这样?

最佳答案

仅仅因为您在一次调用 send 中发送了字节并不意味着您将在一次调用 recv 中接收到它们。他们可能会以更小或更大的群体到达。例如。也许你发送:

c.send('one')
c.send('two')
c.send('three')
c.send('finish')

但是你收到了

s.recv(1024) -> 'onetwothreefinish'

或者你可能会收到

s.receive(1024) -> 'one'
s.receive(1024) -> 'two'
s.receive(1024) -> 'three'
s.receive(1024) -> 'fin'
s.receive(1024) -> 'ish'

关于python - if 语句被跳过,即使语句为真,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35735088/

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