通过套接字发送文件python [英] sending file through sockets python

查看:85
本文介绍了通过套接字发送文件python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过套接字发送文件,但是当文件完全发送后,我的服务器已挂起.我尝试了很多方法,但这是我的代码.

I'm trying to send files through sockets but my server is hanging when the file is sent completely. I tried a lot of way but here is my code.

客户:

def download_file(self, file_name):
    with open(file_name, "rb") as file:
        while True:
            l = file.read(64)
            if not l:
                break
            self.s.sendall(l)

服务器:

def download_file(self, file_name):
    downloading = True
    with open(file_name, "wb") as file:
        while downloading:
            l = self.client_socket.recv(64)
            if not l:
                print("downloaded")
                downloading = False
            file.write(l)

在文件完成之前,它似乎一直有效,但是随后我的服务器就再也没有转到"if not l:"了.部分,它只是一直挂着等待数据...

It seems to work until the file is complete, but then my server never goes to the "if not l:" part, it just keeps hanging waiting for data...

推荐答案

您忘记了客户端中的 self.s.close().当前,服务器没有指示没有更多数据跟随,因此它必须等待更多数据.

You forgot to self.s.close() in the client. Currently the server has no indication that not more data follow, so it must wait for more to come.

这篇关于通过套接字发送文件python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆