客户发送图片结束的判断方法

2024-10-04 03:19:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用ftpsockets在python中创建一个文件服务器,其中多个客户机将图像发送到一个服务器。这是我用来在服务器端接收图像的方法:

while True:
    data = client.recv(512)
    if not data:
        break
    file.write(data)

这是我用来将图像发送到服务器的方法:

^{pr2}$

我遇到的问题是,在服务器端while循环从未退出,这意味着代码要么卡在recv调用中,要么if语句永远不为true。在客户端没有问题,循环被正确退出。我听说客户端会向服务器发送一些东西,让它停止,但是if语句似乎没有接收到它。如何让服务器在不关闭连接的情况下停止尝试接收?在


Tags: 文件方法图像服务器clienttrue客户端data
1条回答
网友
1楼 · 发布于 2024-10-04 03:19:28

https://docs.python.org/2/howto/sockets.html#disconnecting

在客户端上,关闭服务器的套接字。在服务器上,检查recv是否返回0字节。在

同样来自documentation for using a socket

When a recv returns 0 bytes, it means the other side has closed (or is in the process of closing) the connection. You will not receive any more data on this connection. Ever. You may be able to send data successfully; I’ll talk more about this later.

相关问题 更多 >