1 | |
---|
2 | pro ftp_post, u, cmd, res, out=out, count=count |
---|
3 | compile_opt idl2 |
---|
4 | if (cmd ne '') then begin |
---|
5 | printf, u, cmd, format='(a)' |
---|
6 | ; |
---|
7 | ; comment out the following line to disable debug info |
---|
8 | print, '>'+cmd |
---|
9 | endif |
---|
10 | if (size(out,/type) eq 0) then out='2?? *' |
---|
11 | catch, err |
---|
12 | if (err ne 0) then return |
---|
13 | line='' |
---|
14 | count=0 |
---|
15 | while arg_present(res) do begin |
---|
16 | readf, u, line |
---|
17 | if count eq 0 then res=line else res=[res,line] |
---|
18 | count=count+1 |
---|
19 | ; |
---|
20 | ; comment out the following line to disable debug info |
---|
21 | print, '<'+line |
---|
22 | if strmatch(line,out) then break |
---|
23 | endwhile |
---|
24 | end |
---|
25 | |
---|
26 | pro ftp_parse_pasv, text, host, port |
---|
27 | t=strtrim(text,2) |
---|
28 | ind=where(strcmp(t,'227',3)) |
---|
29 | i=ind[0] |
---|
30 | if (i ne -1) then begin |
---|
31 | sub=stregex(t[i],'\([0-9,]*\)',/extract) |
---|
32 | p=strsplit(strmid(sub,1,strlen(sub)-2),',',/extract) |
---|
33 | p=strtrim(p,2) |
---|
34 | host=p[0]+'.'+p[1]+'.'+p[2]+'.'+p[3] |
---|
35 | port=256*long(p[4])+long(p[5]) |
---|
36 | endif |
---|
37 | end |
---|
38 | ;+ |
---|
39 | ; |
---|
40 | ; @file_comments |
---|
41 | ; READ_FTP, remote_host [, files] [, directory] [,/FILE] [,DATA=variable] |
---|
42 | ; [,USER=string] [,PASS=string] [,/PTR] |
---|
43 | ; |
---|
44 | ; @param DIR |
---|
45 | ; Remote directory where the files reside on the ftp server |
---|
46 | ; |
---|
47 | ; @param FILES |
---|
48 | ; A single filename or an array of filenames to be retrieved. |
---|
49 | ; |
---|
50 | ; @keyword FILE |
---|
51 | ; Set this keyword to make a local copy of the file to be |
---|
52 | ; transferred. The local file will have the same name as the |
---|
53 | ; remote file and will be placed in the current working |
---|
54 | ; directory. |
---|
55 | ; |
---|
56 | ; @keyword DATA |
---|
57 | ; Set this to a named variable that will contain either a |
---|
58 | ; byte array or an array of pointers to byte arrays with the |
---|
59 | ; transferred data. If there is more than one file, an array |
---|
60 | ; of pointers is returned, one for each file. |
---|
61 | ; Note that when downloading large files using /FILE |
---|
62 | ; instead will require much less memory since the entire file |
---|
63 | ; is not stored in a variable in that case. |
---|
64 | ; |
---|
65 | ; @keyword PTR |
---|
66 | ; Set this keyword to return an array of pointers |
---|
67 | ; even when there is only one file. |
---|
68 | ; |
---|
69 | ; @keyword USER |
---|
70 | ; Specify user name to connect to server with. Default is: anonymous. |
---|
71 | ; |
---|
72 | ; @keyword PASS |
---|
73 | ; Specify password to use when connecting. Default is: test@test.com. |
---|
74 | ; |
---|
75 | ; @examples |
---|
76 | ; 1) Retrieve and print the contents of ftp://ftp.rsinc.com/pub/gzip/README.GZIP: |
---|
77 | ; IDL> READ_FTP, 'ftp://ftp.rsinc.com/pub/gzip/README.GZIP', DATA=data |
---|
78 | ; IDL> help, data |
---|
79 | ; DATA BYTE = Array[2134] |
---|
80 | ; IDL> print, string(data) |
---|
81 | ; ------------------------------------------------------------------------------ |
---|
82 | ; README file: Research Systems Anonymous FTP site (ftp.rsinc.com) |
---|
83 | ; pub directory |
---|
84 | ; gzip directory |
---|
85 | ; ------------------------------------------------------------------------------ |
---|
86 | ; ... |
---|
87 | ; |
---|
88 | ; 2) Retrieve some files from podaac.jpl.nasa.gov and store the files |
---|
89 | ; in the current working directory: |
---|
90 | ; |
---|
91 | ; IDL> files = string(lindgen(10)+50,format='(%"MGB370.%3.3d.gz")') |
---|
92 | ; IDL> READ_FTP, 'podaac.jpl.nasa.gov', files, $ |
---|
93 | ; IDL> 'pub/sea_surface_height/topex_poseidon/mgdrb/data/MGB_370', /FILE |
---|
94 | ; IDL> spawn,'dir MGB*',/log_output |
---|
95 | ; Volume in drive C is Local Disk |
---|
96 | ; Volume Serial Number is 34CE-24DF |
---|
97 | ; |
---|
98 | ; Directory of C:\test\test0307 |
---|
99 | ; |
---|
100 | ; 07/28/2003 11:58a 362,167 MGB370.050.gz |
---|
101 | ; 07/28/2003 11:58a 333,005 MGB370.051.gz |
---|
102 | ; 07/28/2003 11:58a 310,287 MGB370.052.gz |
---|
103 | ; 07/28/2003 11:58a 358,771 MGB370.053.gz |
---|
104 | ; 07/28/2003 11:59a 387,282 MGB370.054.gz |
---|
105 | ; 07/28/2003 11:59a 361,633 MGB370.055.gz |
---|
106 | ; 07/28/2003 11:59a 383,075 MGB370.056.gz |
---|
107 | ; 07/28/2003 11:59a 365,844 MGB370.057.gz |
---|
108 | ; 07/28/2003 11:59a 383,918 MGB370.058.gz |
---|
109 | ; 07/28/2003 12:00p 372,712 MGB370.059.gz |
---|
110 | ; 10 File(s) 3,618,694 bytes |
---|
111 | ; |
---|
112 | ; These compressed files can cosequently be opened with OPENR and the |
---|
113 | ; /COMPRESSED keyword. |
---|
114 | ; |
---|
115 | ; @history |
---|
116 | ; |
---|
117 | ; @version |
---|
118 | ; $Id$ |
---|
119 | ; |
---|
120 | ; @todo |
---|
121 | ; seb: que fait-on de "syntax" au debut du header? |
---|
122 | ; give examples with date in year 0 (should not exists but may happen) |
---|
123 | ; |
---|
124 | ;- |
---|
125 | pro read_ftp, site, files, dir, port, data=data, file=file, user=user, $ |
---|
126 | pass=pass, ptr=ptr |
---|
127 | compile_opt idl2 |
---|
128 | if n_elements(port) eq 0 then port='ftp' |
---|
129 | if n_elements(files) eq 0 then begin |
---|
130 | if strcmp(site,'ftp://',6) then host=strmid(site,6) else host=site |
---|
131 | pos=strpos(host,'/') |
---|
132 | dir=strmid(host,pos) |
---|
133 | host=strmid(host,0,pos) |
---|
134 | pos=strpos(dir,'/',/reverse_search) |
---|
135 | files=strmid(dir,pos+1) |
---|
136 | dir=strmid(dir,0,pos) |
---|
137 | endif else host=site |
---|
138 | if (size(user,/type) eq 0) then user='anonymous' |
---|
139 | if (size(pass,/type) eq 0) then pass='test@test.com' |
---|
140 | ; |
---|
141 | socket, u, host, port, connect_timeout=5, read_timeout=5, /get_lun |
---|
142 | ftp_post, u, '', res |
---|
143 | ftp_post, u, 'USER '+user, res, out='3?? *' |
---|
144 | ftp_post, u, 'PASS '+pass, res |
---|
145 | ftp_post, u, 'TYPE I', res |
---|
146 | if (size(dir,/type) ne 0) then ftp_post, u, 'CWD '+dir, res |
---|
147 | if keyword_set(file) or arg_present(data) then begin |
---|
148 | bufsize=512 |
---|
149 | buffer=bytarr(bufsize) |
---|
150 | n=n_elements(files) |
---|
151 | if arg_present(data) then dat=ptrarr(n) |
---|
152 | for i=0, n-1 do begin |
---|
153 | ftp_post, u, 'SIZE '+files[i], res, out='213 *' |
---|
154 | sz=long64(strmid(res[n_elements(res)-1],4)) |
---|
155 | if arg_present(data) then dat[i]=ptr_new(bytarr(sz)) |
---|
156 | ftp_post, u, 'PASV', res |
---|
157 | ftp_parse_pasv, res, host, port |
---|
158 | ftp_post, u, 'RETR '+files[i], res, out='1?? *' |
---|
159 | socket, v, host, port, connect_timeout=5, read_timeout=5, $ |
---|
160 | /get_lun, /rawio |
---|
161 | tc=0ll |
---|
162 | if keyword_set(file) then openw,w,files[i],/get_lun |
---|
163 | while (tc lt sz) do begin |
---|
164 | if (sz-tc lt bufsize) then begin |
---|
165 | bufsize=sz-tc |
---|
166 | buffer=bytarr(bufsize) |
---|
167 | endif |
---|
168 | readu, v, buffer, transfer_count=dtc |
---|
169 | if arg_present(data) then $ |
---|
170 | (*dat[i])[tc]=(dtc eq bufsize)?buffer:buffer[0:dtc-1] |
---|
171 | if keyword_set(file) then $ |
---|
172 | writeu,w,(dtc eq bufsize)?buffer:buffer[0:dtc-1] |
---|
173 | tc=tc+dtc |
---|
174 | endwhile |
---|
175 | free_lun, v |
---|
176 | if keyword_set(file) then free_lun, w |
---|
177 | ftp_post, u, '', res |
---|
178 | endfor |
---|
179 | if arg_present(data) then begin |
---|
180 | if (n gt 1 or keyword_set(ptr)) then data=dat $ |
---|
181 | else data=temporary(*dat[0]) |
---|
182 | endif |
---|
183 | endif |
---|
184 | ftp_post, u, 'QUIT', res |
---|
185 | free_lun, u |
---|
186 | end |
---|