Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

How can I have my web server download a file from another web site? (The file is NOT HTML!) For example, the "site copier" feature some web hosts have, in which you give it a URL and it copies the files to your site. In my situation, I will need to have my server download two VERY large files (i.e. 200MB) every month.

Replies are listed 'Best First'.
Re: Having Web Server Download a File
by chromatic (Archbishop) on May 16, 2002 at 03:47 UTC
    You could open a socket yourself, but that's a solved problem with libwwwperl. (See LWP.) If you're fine downloading them within the server process, you're done. Otherwise, you could set up a daemon that listens for a message of some kind and downloads them itself. You could use a directory, a file, or even a database table for this. You might also look at the chunk feature of LWP::UserAgent, if you're using a persistent process and don't want to eat up 200 megabytes of memory.
Re: Having Web Server Download a File
by mattr (Curate) on May 16, 2002 at 06:47 UTC
    Net::FTP or unix's wget command or lynx or wwwoffle or...

    How about just using lynx -source? short and sweet. Or wget, I've used it to download a file and pictures in it, called from a Perl script daily.

    Or use a program which can resume downloads, I think Xdownload or some others can do that. Assuming you are on unix. Or what was said above. Anyway here is a script of mine called dailywget which I've been using for a couple years.

    #!/usr/local/bin/perl # pick up files from the Internet every day and save them somewhere # with date stamp. @urlstoget = qw ( http://your.urls.here ); $rootdir = "/annex/work/dailywget/dls/"; foreach $u (@urlstoget) { $t = scalar(localtime); # Tue Apr 4 02:59:02 JST 2000 $date = substr($t,20,4) . "." . substr($t, 4,3) . "." . substr($t, 8,2); $date =~ s/\s//g; $filename = "solution_" . $date . ".html"; &geturl($u,$filename); print "Retrieved $filename.\n"; } chdir $rootdir; exit 0; sub geturl { my ($url,$f) = @_; chdir $rootdir; $cmd = "wget -t 5 -O - $url > $f"; print "Executing $cmd\n"; @args = ($cmd); # wget 10 tries, url to stdout #foreach $a(@args) {print "-> $a\n";} system(@args); # you can print several urls on one line }

      Forking is expensive. Avoid it if you're on a loaded machine, or expect the computer to become loaded (always expect your site to grow large, at least in code design).
      Mirroring a single file is easy:

      use LWP::Simple; mirror($url, $filename);
      Undoubtedly, there are recursive solutions on CPAN.

      - Yes, I reinvent wheels.
      - Spam: Visit eurotraQ.
      

Re: Having Web Server Download a File
by Anonymous Monk on May 16, 2002 at 04:58 UTC
    It's me again...

    P.S. I'm still consider myself a newbie.