簡體   English   中英

通過 sockets python 發送文件

[英]sending file through sockets python

我正在嘗試通過 sockets 發送文件,但是當文件完全發送時我的服務器掛起。 我嘗試了很多方法,但這是我的代碼。

客戶:

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)

它似乎在文件完成之前工作,但是我的服務器永遠不會進入“如果不是 l:”部分,它只是一直掛起等待數據......

您忘記在客戶端中進行self.s.close()了。 目前服務器沒有跡象表明沒有更多的數據跟隨,所以它必須等待更多的數據到來。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM