🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: pl.comp.lang.python
Article #267

Re: połączenia http - ściąganie plików

#267
From: Adam Karpierz
Date: Sat, 07 Dec 2002 19:40
60 lines
2895 bytes
Użytkownik "Marcin Wojtczuk" <fastviper@telkab.pl> napisał:

> Jakie rozwiązanie proponujecie ? (Błagam, niech to nie będą obiekty).

Jelsli _na sile_ nie chcesz obiektow to zastanow sie jednak nad zmiana
jezyka.
REXX moze byc tu nie najgorszy.
W Pythonie od obiektow nei uciekniesz, a bez nich pisanie w tym jezyku
to wlasciwie masochizm niestety.

> W ostateczności mogę na socketach operować, ale może jest jakieś httpget()
?
> Problemem może być timeout - trzeba to wysoko ustawić. Drugi problem to
> sygnalizowanie zerwanego połączenia - nie może być tak, że plik się
> ściągnął do połowy.
>
> Interesuje mnie też jak najefektywniej zapisać do pliku dużą ilość
> danych (6-40kB).

Powinno Ci troche pomoc, a zerwanie polaczenia czy inne bledy
sobie obsluzysz przechwutujac odpowiednie wyjatki:

def wget(url, dstDir=None):
    if dstDir is None: dstDir = ''
    pth = urlparse.urlparse(url)[2]
    arch = pth[pth.rfind('/') + 1:]
    outfile = join(dstDir, arch)
    ident  = 5
    format = '%' + str(ident) + 'dkB'
    clear  = (ident + 2 + 1) * '\b' + (ident + 2 + 1) * ' ' + (ident + 2 +
1) * '\b'
    msg = 'Downloading the "%s"...' % arch
    print msg.ljust(67),
    istream = urllib.urlopen(url)
    try:
        ostream = open(outfile, 'wb')
        try:
            cnt = 0
            print format % cnt,
            while 1:
                buff = istream.read(1024)
                if not buff:
                    break
                ostream.write(buff)
                cnt += 1
                print clear + format % cnt,
        except:
            ostream.close()
            if os.path.isfile(outfile):
                os.remove(outfile)
            print clear, 'Error!'
            raise
        ostream.close()
        print clear, 'Done'
    finally:
        istream.close()


--
Adam Karpierz
Technical Details
Message-ID: <astj5c$3h1$1@news.tpi.pl>
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!pingwin.icm.edu.pl!mat.uni.torun.pl!news.man.torun.pl!news.man.poznan.pl!newsfeed.tpinternet.pl!news.tpi.pl!not-for-mail
References: <37ersa-64l.ln@fastviper.viper.lan>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!pingwin.icm.edu.pl!mat.uni.torun.pl!news.man.torun.pl!news.man.poznan.pl!newsfeed.tpinternet.pl!news.tpi.pl!not-for-mail
From: "Adam Karpierz" <karpierz@_NOSPAM_alpha.pl>
Newsgroups: pl.comp.lang.python
Subject: Re: połączenia http -
 ściąganie plików
Date: Sat, 7 Dec 2002 20:40:37 +0100
Organization: tp.internet - http://www.tpi.pl/
Lines: 63
Message-ID: <astj5c$3h1$1@news.tpi.pl>
References: <37ersa-64l.ln@fastviper.viper.lan>
NNTP-Posting-Host: ppp204.intertele.pl
X-Trace: news.tpi.pl 1039290353 3617 194.204.186.204 (7 Dec 2002 19:45:53 GMT)
X-Complaints-To: usenet@tpi.pl
NNTP-Posting-Date: Sat, 7 Dec 2002 19:45:53 +0000 (UTC)
X-Newsreader: Microsoft Outlook Express 6.00.2600.0000
X-MSMail-Priority: Normal
X-Priority: 3
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit