Wiki

root/cobra/trunk/Samples/Download.cobra

Revision 2129, 1.3 KB (checked in by Chuck.Esterbrook, 3 years ago)

Take out is shared on def main in the how-to's.

  • Property svn:eol-style set to native
Line 
1"""
2download.cobra
3
4Downloads a file specified by a URL to the local file system.
5
6Windows:
7> cd \path\to\directory\of\download.cobra
8> cobra -c download.cobra
9> download http://www.google.com/images/logo_sm.gif
10Downloading http://www.google.com/images/logo_sm.gif to logo_sm.gif
11Done.
12
13Unix-like:
14> cd /path/to/directory/of/download.cobra
15> cobra -c Download.cobra
16> mono download.exe http://www.google.com/images/logo_sm.gif
17Downloading http://www.google.com/images/logo_sm.gif to logo_sm.gif
18Done.
19
20You can specify the local filename:
21> download http://www.google.com/images/logo_sm.gif GoogleLogo.gif
22Downloading http://www.google.com/images/logo_sm.gif to GoogleLogo.gif
23Done.
24
25"""
26
27use System.Net
28
29class DownloadFile
30
31    def main
32        args = CobraCore.commandLineArgs
33        if args.count < 2
34            print 'usage: download URL \[LOCALFILENAME]'
35            return
36        url = args[1]
37
38        # localFileName is derived from the url if missing:
39        if args.count > 2
40            localFileName = args[2]
41        else
42            s = url
43            if s.endsWith('/'), s = s[:-1]
44            i = s.lastIndexOf('/')
45            if i <> -1, localFileName = s[i+1:]
46            else, print 'Invalid URL.'
47
48        print 'Downloading [url] to [localFileName]'
49        Console.out.flush
50
51        try
52            WebClient().downloadFile(url, localFileName)
53        catch e as Exception
54            print e
55
56        print 'Done.'
Note: See TracBrowser for help on using the browser.