Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

#$Id: download.py 91 2011-02-07 23:01:27Z sarkiss $ 

import wx 

import urllib 

import os 

def download(url, dest): 

    dlg = wx.ProgressDialog("Download Progress", 

                       "Please wait...", 

                           style = wx.PD_CAN_ABORT 

                            | wx.PD_APP_MODAL 

                            | wx.PD_ELAPSED_TIME 

                            | wx.PD_ESTIMATED_TIME 

                            ) 

    dlg.Update(0, "Please Wait...") 

    fURL = urllib.urlopen(url) 

    header = fURL.info() 

    size = None 

    max = 100 

    outFile = open(dest, 'wb') 

    keepGoing = True 

    if "content-length" in header: 

        size = int(header["Content-Length"]) 

        kBytes = size/1024 

        downloadBytes = size/max 

        count = 0 

        while keepGoing: 

            count += 1 

            if count >= max: 

                count  = 99 

            (keepGoing, skip) = dlg.Update(count, "Downloaded "+str(count*downloadBytes/1024)+" of "+ str(kBytes)+"KB") 

            b = fURL.read(downloadBytes) 

            if b: 

                outFile.write(b) 

            else: 

                break 

    else: 

            while keepGoing: 

                (keepGoing, skip) = dlg.UpdatePulse() 

                b = fURL.read(1024*8) 

                if b: 

                    outFile.write(b) 

                else: 

                    break 

    outFile.close() 

 

    dlg.Update(99, "Downloaded "+ str(os.path.getsize(dest)/1024)+"KB") 

    dlg.Destroy() 

    return  keepGoing 

 

if __name__ == "__main__": 

    app = wx.App() 

    download('http://voxel.dl.sourceforge.net/sourceforge/wxpython/wxPython-docs-2.8.9.2.tar.bz2' 

             , 'wxPython-docs-2.8.9.2.tar.bz2')