简体   繁体   English

检查 python 中文件何时完成下载(套接字编程)

[英]Check when file is finished downloading in python (socket programming)

How could I keep track of the progress of a file download using socket programming with TCP client/server logic to continue code flow without a infinite while loop?如何使用带有 TCP 客户端/服务器逻辑的套接字编程来跟踪文件下载的进度,以在没有无限 while 循环的情况下继续代码流?

If you can parse the headers manually you can get the file size, in content-length field, total iterations will be content_length / recv_buffer_size.如果您可以手动解析标头,您可以获得文件大小,在content-length字段中,总迭代次数将为 content_length / recv_buffer_size。 For example if you receive 1024 bytes at a time and file size is 4096, then number of iterations = 4096/1024 = 4.例如,如果您一次接收 1024 个字节并且文件大小为 4096,则迭代次数 = 4096/1024 = 4。

    total_length = content_length / buffer_size
    progressbar.set_length(total_length)   #some imaginary library
    for i in range(total_length):
      data = socket.recv(buffer_size)
      if data :
        file.write(data)
        progressbar.update(1)   #some imaginary progress-bar library
    file.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM