Chcialbym podczas pobierania pliku z sieci dynamicznie okreslac jego wielkosc.
Oczywiscie poberanie pliku odbywa sie przez funkcje pythona.
Chcialem sprobowac stworzyc malego managera, ktory bedzie pokazywal pasek postepu i wlasnie do tego potrzebuje okreslac wielkosc.
			
			
									
						
										
						[+][Python] Okreslenie wielkosci pliku podczas pobierania
Zakładam, że pobierasz plik po http.
W Content-Length masz wielkość pliku, więc zostaje Ci tylko w pętli sprawdzać ile już pobrałeś.
			
			
									
						
										
						Kod: Zaznacz cały
db@yennefer:~$ telnet localhost 80
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
GET /test HTTP/1.0
Host: localhost
HTTP/1.1 200 OK
Date: Wed, 12 Mar 2008 11:50:40 GMT
Server: Apache/2.2.8 (Debian) PHP/5.2.5-3 with Suhosin-Patch
Last-Modified: Wed, 12 Mar 2008 08:26:00 GMT
ETag: "8fc43-62d1-448392fda1600"
Accept-Ranges: bytes
Content-Length: 25297
[...]
ale ile to ma wspolnego z funkcja pythona?
Udalo mi sie rozwiazac moj problem:
Jak by ktos tez tego szukal.
Pozdrawiam
			
			
									
						
										
						Udalo mi sie rozwiazac moj problem:
Kod: Zaznacz cały
 
import re
import os
import urllib
import shelve
import sqlite3
import pickle
from glob import glob, iglob
from hashlib import md5
from sys import platform, stderr
from time import time
from xml.dom import minidom
from time import time as xtime, sleep
class Downloader(object):
    '''Shows a progress bar for downloads. this is actually useful outside the
    scope of danbooru.py'''
    
    before = .0
    history = []
    cycles = 0
    average = lambda self: sum(self.history) / (len(self.history) or 1)
    def __init__(self, width=55):
        self.width = width
        self.kibi = lambda bits: bits / 2 ** 10
        self.proc = lambda a, b: a / (b * 0.01)
    def retrieve(self, url, destination, callback=None):
        self.size = 0
        xtime()
        try: urllib.urlretrieve(url, destination, self.progress)
        except KeyboardInterrupt:
            print '\nDownload cancelled'
            for i in range(5):
                try:
                    os.remove(destination)
                    break
                except:
                    sleep(.1)
            else: raise
            if callback: callback()
            exit()
        print
        return self.size
    def progress(self, blocks, blocksize, filesize):
        self.cycles += 1
        bits = min(blocks*blocksize, filesize)
        done = self.proc(bits, filesize) if bits != filesize else 100
        bar = self.bar(done)
        if not self.cycles % 3 and bits != filesize:
            now = xtime()
            elapsed = now-self.before
            if elapsed:
                speed = self.kibi(blocksize * 3 / elapsed)
                self.history.append(speed)
                self.history = self.history[-4:]
            self.before = now
        average = round(sum(self.history[-4:]) / 4, 1)
        self.size = self.kibi(bits)
        print '\r[%s] %s KiB/s  ' % (bar, str(average)),
    def bar(self, done):
        span = self.width * done * 0.01
        offset = len(str(int(done))) - .99
        result = ('%d%%' % (done,)).center(self.width)
        return result.replace(' ', '-', int(span - offset))
        Pozdrawiam