4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the Revised BSD License.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 Revised BSD License for more details.
12 Copyright 2016-2023 Game Maker 2k - https://github.com/GameMaker2k
13 Copyright 2016-2023 Kazuki Przyborowski - https://github.com/KazukiPrzyborowski
15 $FileInfo: pywwwget.py - Last Update: 10/5/2023 Ver. 2.0.2 RC 1 - Author: cooldude2k $
18 from __future__
import division
, absolute_import
, print_function
;
19 import re
, os
, sys
, hashlib
, shutil
, platform
, tempfile
, urllib
, zlib
, bz2
, time
, argparse
, subprocess
, socket
, email
.utils
, datetime
, time
;
20 import logging
as log
;
21 from ftplib
import FTP
, FTP_TLS
;
22 from base64
import b64encode
;
24 from cgi
import parse_qsl
;
27 from urlparse
import parse_qsl
;
29 from urllib
.parse
import parse_qsl
;
30 except (DeprecationWarning, TypeError):
32 from urlparse
import parse_qsl
;
34 from urllib
.parse
import parse_qsl
;
41 havemechanize
= False;
46 havemechanize
= False;
73 from httplib2
import HTTPConnectionWithTimeout
, HTTPSConnectionWithTimeout
;
113 if(sys
.version
[0]=="2"):
115 from io
import StringIO
, BytesIO
;
118 from cStringIO
import StringIO
;
119 from cStringIO
import StringIO
as BytesIO
;
121 from StringIO
import StringIO
;
122 from StringIO
import StringIO
as BytesIO
;
123 # From http://python-future.org/compatible_idioms.html
124 from urlparse
import urlparse
, urlunparse
, urlsplit
, urlunsplit
, urljoin
;
125 from urllib
import urlencode
;
126 from urllib
import urlopen
as urlopenalt
;
127 from urllib2
import urlopen
, Request
, install_opener
, HTTPError
, URLError
, build_opener
, HTTPCookieProcessor
;
128 import urlparse
, cookielib
;
129 from httplib
import HTTPConnection
, HTTPSConnection
;
130 if(sys
.version
[0]>="3"):
131 from io
import StringIO
, BytesIO
;
132 # From http://python-future.org/compatible_idioms.html
133 from urllib
.parse
import urlparse
, urlunparse
, urlsplit
, urlunsplit
, urljoin
, urlencode
;
134 from urllib
.request
import urlopen
, Request
, install_opener
, build_opener
, HTTPCookieProcessor
;
135 from urllib
.error
import HTTPError
, URLError
;
136 import urllib
.parse
as urlparse
;
137 import http
.cookiejar
as cookielib
;
138 from http
.client
import HTTPConnection
, HTTPSConnection
;
140 __program_name__
= "PyWWW-Get";
141 __program_alt_name__
= "PyWWWGet";
142 __program_small_name__
= "wwwget";
143 __project__
= __program_name__
;
144 __project_url__
= "https://github.com/GameMaker2k/PyWWW-Get";
145 __version_info__
= (2, 0, 2, "RC 1", 1);
146 __version_date_info__
= (2023, 10, 5, "RC 1", 1);
147 __version_date__
= str(__version_date_info__
[0])+"."+str(__version_date_info__
[1]).zfill(2)+"."+str(__version_date_info__
[2]).zfill(2);
148 __revision__
= __version_info__
[3];
149 __revision_id__
= "$Id$";
150 if(__version_info__
[4] is not None):
151 __version_date_plusrc__
= __version_date__
+"-"+str(__version_date_info__
[4]);
152 if(__version_info__
[4] is None):
153 __version_date_plusrc__
= __version_date__
;
154 if(__version_info__
[3] is not None):
155 __version__
= str(__version_info__
[0])+"."+str(__version_info__
[1])+"."+str(__version_info__
[2])+" "+str(__version_info__
[3]);
156 if(__version_info__
[3] is None):
157 __version__
= str(__version_info__
[0])+"."+str(__version_info__
[1])+"."+str(__version_info__
[2]);
159 tmpfileprefix
= "py"+str(sys
.version_info
[0])+__program_small_name__
+str(__version_info__
[0])+"-";
161 pytempdir
= tempfile
.gettempdir();
163 PyBitness
= platform
.architecture();
164 if(PyBitness
=="32bit" or PyBitness
=="32"):
166 elif(PyBitness
=="64bit" or PyBitness
=="64"):
171 compression_supported_list
= ['identity', 'gzip', 'deflate', 'bzip2'];
173 compression_supported_list
.append('br');
175 compression_supported_list
.append('zstd');
177 compression_supported_list
.append('lzma');
178 compression_supported_list
.append('xz');
179 compression_supported
= ', '.join(compression_supported_list
);
181 geturls_cj
= cookielib
.CookieJar();
182 windowsNT4_ua_string
= "Windows NT 4.0";
183 windowsNT4_ua_addon
= {'SEC-CH-UA-PLATFORM': "Windows", 'SEC-CH-UA-ARCH': "x86", 'SEC-CH-UA-BITNESS': "32", 'SEC-CH-UA-PLATFORM': "4.0.0"};
184 windows2k_ua_string
= "Windows NT 5.0";
185 windows2k_ua_addon
= {'SEC-CH-UA-PLATFORM': "Windows", 'SEC-CH-UA-ARCH': "x86", 'SEC-CH-UA-BITNESS': "32", 'SEC-CH-UA-PLATFORM': "5.0.0"};
186 windowsXP_ua_string
= "Windows NT 5.1";
187 windowsXP_ua_addon
= {'SEC-CH-UA-PLATFORM': "Windows", 'SEC-CH-UA-ARCH': "x86", 'SEC-CH-UA-BITNESS': "32", 'SEC-CH-UA-PLATFORM': "5.1.0"};
188 windowsXP64_ua_string
= "Windows NT 5.2; Win64; x64";
189 windowsXP64_ua_addon
= {'SEC-CH-UA-PLATFORM': "Windows", 'SEC-CH-UA-ARCH': "x86", 'SEC-CH-UA-BITNESS': "64", 'SEC-CH-UA-PLATFORM': "5.1.0"};
190 windows7_ua_string
= "Windows NT 6.1; Win64; x64";
191 windows7_ua_addon
= {'SEC-CH-UA-PLATFORM': "Windows", 'SEC-CH-UA-ARCH': "x86", 'SEC-CH-UA-BITNESS': "64", 'SEC-CH-UA-PLATFORM': "6.1.0"};
192 windows8_ua_string
= "Windows NT 6.2; Win64; x64";
193 windows8_ua_addon
= {'SEC-CH-UA-PLATFORM': "Windows", 'SEC-CH-UA-ARCH': "x86", 'SEC-CH-UA-BITNESS': "64", 'SEC-CH-UA-PLATFORM': "6.2.0"};
194 windows81_ua_string
= "Windows NT 6.3; Win64; x64";
195 windows81_ua_addon
= {'SEC-CH-UA-PLATFORM': "Windows", 'SEC-CH-UA-ARCH': "x86", 'SEC-CH-UA-BITNESS': "64", 'SEC-CH-UA-PLATFORM': "6.3.0"};
196 windows10_ua_string
= "Windows NT 10.0; Win64; x64";
197 windows10_ua_addon
= {'SEC-CH-UA-PLATFORM': "Windows", 'SEC-CH-UA-ARCH': "x86", 'SEC-CH-UA-BITNESS': "64", 'SEC-CH-UA-PLATFORM': "10.0.0"};
198 windows11_ua_string
= "Windows NT 11.0; Win64; x64";
199 windows11_ua_addon
= {'SEC-CH-UA-PLATFORM': "Windows", 'SEC-CH-UA-ARCH': "x86", 'SEC-CH-UA-BITNESS': "64", 'SEC-CH-UA-PLATFORM': "11.0.0"};
200 geturls_ua_firefox_windows7
= "Mozilla/5.0 ("+windows7_ua_string
+"; rv:109.0) Gecko/20100101 Firefox/117.0";
201 geturls_ua_seamonkey_windows7
= "Mozilla/5.0 ("+windows7_ua_string
+"; rv:91.0) Gecko/20100101 Firefox/91.0 SeaMonkey/2.53.17";
202 geturls_ua_chrome_windows7
= "Mozilla/5.0 ("+windows7_ua_string
+") AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36";
203 geturls_ua_chromium_windows7
= "Mozilla/5.0 ("+windows7_ua_string
+") AppleWebKit/537.36 (KHTML, like Gecko) Chromium/117.0.0.0 Chrome/117.0.0.0 Safari/537.36";
204 geturls_ua_palemoon_windows7
= "Mozilla/5.0 ("+windows7_ua_string
+"; rv:102.0) Gecko/20100101 Goanna/6.3 Firefox/102.0 PaleMoon/32.4.0.1";
205 geturls_ua_opera_windows7
= "Mozilla/5.0 ("+windows7_ua_string
+") AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 OPR/102.0.0.0";
206 geturls_ua_vivaldi_windows7
= "Mozilla/5.0 ("+windows7_ua_string
+") AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 Vivaldi/6.2.3105.48";
207 geturls_ua_internet_explorer_windows7
= "Mozilla/5.0 ("+windows7_ua_string
+"; Trident/7.0; rv:11.0) like Gecko";
208 geturls_ua_microsoft_edge_windows7
= "Mozilla/5.0 ("+windows7_ua_string
+") AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36 Edg/117.0.2045.31";
209 geturls_ua_pywwwget_python
= "Mozilla/5.0 (compatible; {proname}/{prover}; +{prourl})".format(proname
=__project__
, prover
=__version__
, prourl
=__project_url__
);
210 if(platform
.python_implementation()!=""):
211 py_implementation
= platform
.python_implementation();
212 if(platform
.python_implementation()==""):
213 py_implementation
= "Python";
214 geturls_ua_pywwwget_python_alt
= "Mozilla/5.0 ({osver}; {archtype}; +{prourl}) {pyimp}/{pyver} (KHTML, like Gecko) {proname}/{prover}".format(osver
=platform
.system()+" "+platform
.release(), archtype
=platform
.machine(), prourl
=__project_url__
, pyimp
=py_implementation
, pyver
=platform
.python_version(), proname
=__project__
, prover
=__version__
);
215 geturls_ua_googlebot_google
= "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)";
216 geturls_ua_googlebot_google_old
= "Googlebot/2.1 (+http://www.google.com/bot.html)";
217 geturls_ua
= geturls_ua_firefox_windows7
;
218 geturls_headers_firefox_windows7
= {'Referer': "http://google.com/", 'User-Agent': geturls_ua_firefox_windows7
, 'Accept-Encoding': compression_supported
, 'Accept-Language': "en-US,en;q=0.8,en-CA,en-GB;q=0.6", 'Accept-Charset': "ISO-8859-1,ISO-8859-15,utf-8;q=0.7,*;q=0.7", 'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 'Connection': "close"};
219 geturls_headers_seamonkey_windows7
= {'Referer': "http://google.com/", 'User-Agent': geturls_ua_seamonkey_windows7
, 'Accept-Encoding': compression_supported
, 'Accept-Language': "en-US,en;q=0.8,en-CA,en-GB;q=0.6", 'Accept-Charset': "ISO-8859-1,ISO-8859-15,utf-8;q=0.7,*;q=0.7", 'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 'Connection': "close"};
220 geturls_headers_chrome_windows7
= {'Referer': "http://google.com/", 'User-Agent': geturls_ua_chrome_windows7
, 'Accept-Encoding': compression_supported
, 'Accept-Language': "en-US,en;q=0.8,en-CA,en-GB;q=0.6", 'Accept-Charset': "ISO-8859-1,ISO-8859-15,utf-8;q=0.7,*;q=0.7", 'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 'Connection': "close", 'SEC-CH-UA': "\"Google Chrome\";v=\"117\", \"Not;A=Brand\";v=\"8\", \"Chromium\";v=\"117\"", 'SEC-CH-UA-FULL-VERSION': "117.0.5938.63"};
221 geturls_headers_chrome_windows7
.update(windows7_ua_addon
);
222 geturls_headers_chromium_windows7
= {'Referer': "http://google.com/", 'User-Agent': geturls_ua_chromium_windows7
, 'Accept-Encoding': compression_supported
, 'Accept-Language': "en-US,en;q=0.8,en-CA,en-GB;q=0.6", 'Accept-Charset': "ISO-8859-1,ISO-8859-15,utf-8;q=0.7,*;q=0.7", 'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 'Connection': "close", 'SEC-CH-UA': "\"Chromium\";v=\"117\", \"Not;A=Brand\";v=\"24\"", 'SEC-CH-UA-FULL-VERSION': "117.0.5938.63"};
223 geturls_headers_chromium_windows7
.update(windows7_ua_addon
);
224 geturls_headers_palemoon_windows7
= {'Referer': "http://google.com/", 'User-Agent': geturls_ua_palemoon_windows7
, 'Accept-Encoding': compression_supported
, 'Accept-Language': "en-US,en;q=0.8,en-CA,en-GB;q=0.6", 'Accept-Charset': "ISO-8859-1,ISO-8859-15,utf-8;q=0.7,*;q=0.7", 'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 'Connection': "close"};
225 geturls_headers_opera_windows7
= {'Referer': "http://google.com/", 'User-Agent': geturls_ua_opera_windows7
, 'Accept-Encoding': compression_supported
, 'Accept-Language': "en-US,en;q=0.8,en-CA,en-GB;q=0.6", 'Accept-Charset': "ISO-8859-1,ISO-8859-15,utf-8;q=0.7,*;q=0.7", 'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 'Connection': "close", 'SEC-CH-UA': "\"Chromium\";v=\"116\", \"Not;A=Brand\";v=\"8\", \"Opera\";v=\"102\"", 'SEC-CH-UA-FULL-VERSION': "102.0.4880.56"};
226 geturls_headers_opera_windows7
.update(windows7_ua_addon
);
227 geturls_headers_vivaldi_windows7
= {'Referer': "http://google.com/", 'User-Agent': geturls_ua_vivaldi_windows7
, 'Accept-Encoding': compression_supported
, 'Accept-Language': "en-US,en;q=0.8,en-CA,en-GB;q=0.6", 'Accept-Charset': "ISO-8859-1,ISO-8859-15,utf-8;q=0.7,*;q=0.7", 'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 'Connection': "close", 'SEC-CH-UA': "\"Google Chrome\";v=\"117\", \"Not;A=Brand\";v=\"8\", \"Vivaldi\";v=\"6.2\"", 'SEC-CH-UA-FULL-VERSION': "6.2.3105.48"};
228 geturls_headers_vivaldi_windows7
.update(windows7_ua_addon
);
229 geturls_headers_internet_explorer_windows7
= {'Referer': "http://google.com/", 'User-Agent': geturls_ua_internet_explorer_windows7
, 'Accept-Encoding': compression_supported
, 'Accept-Language': "en-US,en;q=0.8,en-CA,en-GB;q=0.6", 'Accept-Charset': "ISO-8859-1,ISO-8859-15,utf-8;q=0.7,*;q=0.7", 'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 'Connection': "close"};
230 geturls_headers_microsoft_edge_windows7
= {'Referer': "http://google.com/", 'User-Agent': geturls_ua_microsoft_edge_windows7
, 'Accept-Encoding': compression_supported
, 'Accept-Language': "en-US,en;q=0.8,en-CA,en-GB;q=0.6", 'Accept-Charset': "ISO-8859-1,ISO-8859-15,utf-8;q=0.7,*;q=0.7", 'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 'Connection': "close", 'SEC-CH-UA': "\"Microsoft Edge\";v=\"117\", \"Not;A=Brand\";v=\"8\", \"Chromium\";v=\"117\"", 'SEC-CH-UA-FULL-VERSION': "117.0.2045.31"}
231 geturls_headers_microsoft_edge_windows7
.update(windows7_ua_addon
);
232 geturls_headers_pywwwget_python
= {'Referer': "http://google.com/", 'User-Agent': geturls_ua_pywwwget_python
, 'Accept-Encoding': "none", 'Accept-Language': "en-US,en;q=0.8,en-CA,en-GB;q=0.6", 'Accept-Charset': "ISO-8859-1,ISO-8859-15,utf-8;q=0.7,*;q=0.7", 'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 'Connection': "close", 'SEC-CH-UA': "\""+__project__
+"\";v=\""+str(__version__
)+"\", \"Not;A=Brand\";v=\"8\", \""+py_implementation
+"\";v=\""+str(platform
.release())+"\"", 'SEC-CH-UA-FULL-VERSION': str(__version__
), 'SEC-CH-UA-PLATFORM': ""+py_implementation
+"", 'SEC-CH-UA-ARCH': ""+platform
.machine()+"", 'SEC-CH-UA-PLATFORM': str(__version__
), 'SEC-CH-UA-BITNESS': str(PyBitness
)};
233 geturls_headers_pywwwget_python_alt
= {'Referer': "http://google.com/", 'User-Agent': geturls_ua_pywwwget_python_alt
, 'Accept-Encoding': "none", 'Accept-Language': "en-US,en;q=0.8,en-CA,en-GB;q=0.6", 'Accept-Charset': "ISO-8859-1,ISO-8859-15,utf-8;q=0.7,*;q=0.7", 'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 'Connection': "close", 'SEC-CH-UA': "\""+__project__
+"\";v=\""+str(__version__
)+"\", \"Not;A=Brand\";v=\"8\", \""+py_implementation
+"\";v=\""+str(platform
.release())+"\"", 'SEC-CH-UA-FULL-VERSION': str(__version__
), 'SEC-CH-UA-PLATFORM': ""+py_implementation
+"", 'SEC-CH-UA-ARCH': ""+platform
.machine()+"", 'SEC-CH-UA-PLATFORM': str(__version__
), 'SEC-CH-UA-BITNESS': str(PyBitness
)};
234 geturls_headers_googlebot_google
= {'Referer': "http://google.com/", 'User-Agent': geturls_ua_googlebot_google
, 'Accept-Encoding': "none", 'Accept-Language': "en-US,en;q=0.8,en-CA,en-GB;q=0.6", 'Accept-Charset': "ISO-8859-1,ISO-8859-15,utf-8;q=0.7,*;q=0.7", 'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 'Connection': "close"};
235 geturls_headers_googlebot_google_old
= {'Referer': "http://google.com/", 'User-Agent': geturls_ua_googlebot_google_old
, 'Accept-Encoding': "none", 'Accept-Language': "en-US,en;q=0.8,en-CA,en-GB;q=0.6", 'Accept-Charset': "ISO-8859-1,ISO-8859-15,utf-8;q=0.7,*;q=0.7", 'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 'Connection': "close"};
236 geturls_headers
= geturls_headers_firefox_windows7
;
237 geturls_download_sleep
= 0;
239 def verbose_printout(dbgtxt
, outtype
="log", dbgenable
=True, dgblevel
=20):
240 if(outtype
=="print" and dbgenable
):
243 elif(outtype
=="log" and dbgenable
):
244 logging
.info(dbgtxt
);
246 elif(outtype
=="warning" and dbgenable
):
247 logging
.warning(dbgtxt
);
249 elif(outtype
=="error" and dbgenable
):
250 logging
.error(dbgtxt
);
252 elif(outtype
=="critical" and dbgenable
):
253 logging
.critical(dbgtxt
);
255 elif(outtype
=="exception" and dbgenable
):
256 logging
.exception(dbgtxt
);
258 elif(outtype
=="logalt" and dbgenable
):
259 logging
.log(dgblevel
, dbgtxt
);
261 elif(outtype
=="debug" and dbgenable
):
262 logging
.debug(dbgtxt
);
270 def verbose_printout_return(dbgtxt
, outtype
="log", dbgenable
=True, dgblevel
=20):
271 dbgout
= verbose_printout(dbgtxt
, outtype
, dbgenable
, dgblevel
);
276 def add_url_param(url
, **params
):
278 parts
= list(urlparse
.urlsplit(url
));
279 d
= dict(parse_qsl(parts
[n
])); # use cgi.parse_qs for list values
281 parts
[n
]=urlencode(d
);
282 return urlparse
.urlunsplit(parts
);
284 os
.environ
["PATH"] = os
.environ
["PATH"] + os
.pathsep
+ os
.path
.dirname(os
.path
.realpath(__file__
)) + os
.pathsep
+ os
.getcwd();
285 def which_exec(execfile):
286 for path
in os
.environ
["PATH"].split(":"):
287 if os
.path
.exists(path
+ "/" + execfile):
288 return path
+ "/" + execfile;
290 def listize(varlist
):
298 newlistreg
.update({ilx
: varlist
[il
]});
299 newlistrev
.update({varlist
[il
]: ilx
});
302 newlistfull
= {1: newlistreg
, 2: newlistrev
, 'reg': newlistreg
, 'rev': newlistrev
};
305 def twolistize(varlist
):
315 newlistnamereg
.update({ilx
: varlist
[il
][0].strip()});
316 newlistnamerev
.update({varlist
[il
][0].strip(): ilx
});
317 newlistdescreg
.update({ilx
: varlist
[il
][1].strip()});
318 newlistdescrev
.update({varlist
[il
][1].strip(): ilx
});
321 newlistnametmp
= {1: newlistnamereg
, 2: newlistnamerev
, 'reg': newlistnamereg
, 'rev': newlistnamerev
};
322 newlistdesctmp
= {1: newlistdescreg
, 2: newlistdescrev
, 'reg': newlistdescreg
, 'rev': newlistdescrev
};
323 newlistfull
= {1: newlistnametmp
, 2: newlistdesctmp
, 'name': newlistnametmp
, 'desc': newlistdesctmp
}
326 def arglistize(proexec
, *varlist
):
330 newarglist
= [proexec
];
332 if varlist
[il
][0] is not None:
333 newarglist
.append(varlist
[il
][0]);
334 if varlist
[il
][1] is not None:
335 newarglist
.append(varlist
[il
][1]);
339 def fix_header_names(header_dict
):
340 if(sys
.version
[0]=="2"):
341 header_dict
= {k
.title(): v
for k
, v
in header_dict
.iteritems()};
342 if(sys
.version
[0]>="3"):
343 header_dict
= {k
.title(): v
for k
, v
in header_dict
.items()};
346 # hms_string by ArcGIS Python Recipes
347 # https://arcpy.wordpress.com/2012/04/20/146/
348 def hms_string(sec_elapsed
):
349 h
= int(sec_elapsed
/ (60 * 60));
350 m
= int((sec_elapsed
% (60 * 60)) / 60);
351 s
= sec_elapsed
% 60.0;
352 return "{}:{:>02}:{:>05.2f}".format(h
, m
, s
);
354 # get_readable_size by Lipis
355 # http://stackoverflow.com/posts/14998888/revisions
356 def get_readable_size(bytes
, precision
=1, unit
="IEC"):
358 if(unit
!="IEC" and unit
!="SI"):
361 units
= [" B"," KiB"," MiB"," GiB"," TiB"," PiB"," EiB"," ZiB"];
362 unitswos
= ["B","KiB","MiB","GiB","TiB","PiB","EiB","ZiB"];
365 units
= [" B"," kB"," MB"," GB"," TB"," PB"," EB"," ZB"];
366 unitswos
= ["B","kB","MB","GB","TB","PB","EB","ZB"];
371 if abs(bytes
) < unitsize
:
372 strformat
= "%3."+str(precision
)+"f%s";
373 pre_return_val
= (strformat
% (bytes
, unit
));
374 pre_return_val
= re
.sub(r
"([0]+) ([A-Za-z]+)", r
" \2", pre_return_val
);
375 pre_return_val
= re
.sub(r
"\. ([A-Za-z]+)", r
" \1", pre_return_val
);
376 alt_return_val
= pre_return_val
.split();
377 return_val
= {'Bytes': orgbytes
, 'ReadableWithSuffix': pre_return_val
, 'ReadableWithoutSuffix': alt_return_val
[0], 'ReadableSuffix': alt_return_val
[1]}
380 strformat
= "%."+str(precision
)+"f%s";
381 pre_return_val
= (strformat
% (bytes
, "YiB"));
382 pre_return_val
= re
.sub(r
"([0]+) ([A-Za-z]+)", r
" \2", pre_return_val
);
383 pre_return_val
= re
.sub(r
"\. ([A-Za-z]+)", r
" \1", pre_return_val
);
384 alt_return_val
= pre_return_val
.split();
385 return_val
= {'Bytes': orgbytes
, 'ReadableWithSuffix': pre_return_val
, 'ReadableWithoutSuffix': alt_return_val
[0], 'ReadableSuffix': alt_return_val
[1]}
388 def get_readable_size_from_file(infile
, precision
=1, unit
="IEC", usehashes
=False, usehashtypes
="md5,sha1"):
390 usehashtypes
= usehashtypes
.lower();
391 getfilesize
= os
.path
.getsize(infile
);
392 return_val
= get_readable_size(getfilesize
, precision
, unit
);
394 hashtypelist
= usehashtypes
.split(",");
395 openfile
= open(infile
, "rb");
396 filecontents
= openfile
.read();
399 listnumend
= len(hashtypelist
);
400 while(listnumcount
< listnumend
):
401 hashtypelistlow
= hashtypelist
[listnumcount
].strip();
402 hashtypelistup
= hashtypelistlow
.upper();
403 filehash
= hashlib
.new(hashtypelistup
);
404 filehash
.update(filecontents
);
405 filegethash
= filehash
.hexdigest();
406 return_val
.update({hashtypelistup
: filegethash
});
410 def get_readable_size_from_string(instring
, precision
=1, unit
="IEC", usehashes
=False, usehashtypes
="md5,sha1"):
412 usehashtypes
= usehashtypes
.lower();
413 getfilesize
= len(instring
);
414 return_val
= get_readable_size(getfilesize
, precision
, unit
);
416 hashtypelist
= usehashtypes
.split(",");
418 listnumend
= len(hashtypelist
);
419 while(listnumcount
< listnumend
):
420 hashtypelistlow
= hashtypelist
[listnumcount
].strip();
421 hashtypelistup
= hashtypelistlow
.upper();
422 filehash
= hashlib
.new(hashtypelistup
);
423 if(sys
.version
[0]=="2"):
424 filehash
.update(instring
);
425 if(sys
.version
[0]>="3"):
426 filehash
.update(instring
.encode('utf-8'));
427 filegethash
= filehash
.hexdigest();
428 return_val
.update({hashtypelistup
: filegethash
});
432 def http_status_to_reason(code
):
435 101: 'Switching Protocols',
440 203: 'Non-Authoritative Information',
442 205: 'Reset Content',
443 206: 'Partial Content',
445 208: 'Already Reported',
447 300: 'Multiple Choices',
448 301: 'Moved Permanently',
453 307: 'Temporary Redirect',
454 308: 'Permanent Redirect',
457 402: 'Payment Required',
460 405: 'Method Not Allowed',
461 406: 'Not Acceptable',
462 407: 'Proxy Authentication Required',
463 408: 'Request Timeout',
466 411: 'Length Required',
467 412: 'Precondition Failed',
468 413: 'Payload Too Large',
470 415: 'Unsupported Media Type',
471 416: 'Range Not Satisfiable',
472 417: 'Expectation Failed',
473 421: 'Misdirected Request',
474 422: 'Unprocessable Entity',
476 424: 'Failed Dependency',
477 426: 'Upgrade Required',
478 428: 'Precondition Required',
479 429: 'Too Many Requests',
480 431: 'Request Header Fields Too Large',
481 451: 'Unavailable For Legal Reasons',
482 500: 'Internal Server Error',
483 501: 'Not Implemented',
485 503: 'Service Unavailable',
486 504: 'Gateway Timeout',
487 505: 'HTTP Version Not Supported',
488 506: 'Variant Also Negotiates',
489 507: 'Insufficient Storage',
490 508: 'Loop Detected',
492 511: 'Network Authentication Required'
494 return reasons
.get(code
, 'Unknown Status Code');
496 def ftp_status_to_reason(code
):
498 110: 'Restart marker reply',
499 120: 'Service ready in nnn minutes',
500 125: 'Data connection already open; transfer starting',
501 150: 'File status okay; about to open data connection',
503 202: 'Command not implemented, superfluous at this site',
504 211: 'System status, or system help reply',
505 212: 'Directory status',
508 215: 'NAME system type',
509 220: 'Service ready for new user',
510 221: 'Service closing control connection',
511 225: 'Data connection open; no transfer in progress',
512 226: 'Closing data connection',
513 227: 'Entering Passive Mode',
514 230: 'User logged in, proceed',
515 250: 'Requested file action okay, completed',
516 257: '"PATHNAME" created',
517 331: 'User name okay, need password',
518 332: 'Need account for login',
519 350: 'Requested file action pending further information',
520 421: 'Service not available, closing control connection',
521 425: 'Can\'t open data connection',
522 426: 'Connection closed; transfer aborted',
523 450: 'Requested file action not taken',
524 451: 'Requested action aborted. Local error in processing',
525 452: 'Requested action not taken. Insufficient storage space in system',
526 500: 'Syntax error, command unrecognized',
527 501: 'Syntax error in parameters or arguments',
528 502: 'Command not implemented',
529 503: 'Bad sequence of commands',
530 504: 'Command not implemented for that parameter',
531 530: 'Not logged in',
532 532: 'Need account for storing files',
533 550: 'Requested action not taken. File unavailable',
534 551: 'Requested action aborted. Page type unknown',
535 552: 'Requested file action aborted. Exceeded storage allocation',
536 553: 'Requested action not taken. File name not allowed'
538 return reasons
.get(code
, 'Unknown Status Code');
540 def sftp_status_to_reason(code
):
544 2: 'SSH_FX_NO_SUCH_FILE',
545 3: 'SSH_FX_PERMISSION_DENIED',
547 5: 'SSH_FX_BAD_MESSAGE',
548 6: 'SSH_FX_NO_CONNECTION',
549 7: 'SSH_FX_CONNECTION_LOST',
550 8: 'SSH_FX_OP_UNSUPPORTED'
552 return reasons
.get(code
, 'Unknown Status Code');
554 def make_http_headers_from_dict_to_list(headers
={'Referer': "http://google.com/", 'User-Agent': geturls_ua
, 'Accept-Encoding': compression_supported
, 'Accept-Language': "en-US,en;q=0.8,en-CA,en-GB;q=0.6", 'Accept-Charset': "ISO-8859-1,ISO-8859-15,utf-8;q=0.7,*;q=0.7", 'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 'Connection': "close"}):
555 if isinstance(headers
, dict):
557 if(sys
.version
[0]=="2"):
558 for headkey
, headvalue
in headers
.iteritems():
559 returnval
.append((headkey
, headvalue
));
560 if(sys
.version
[0]>="3"):
561 for headkey
, headvalue
in headers
.items():
562 returnval
.append((headkey
, headvalue
));
563 elif isinstance(headers
, list):
569 def make_http_headers_from_dict_to_pycurl(headers
={'Referer': "http://google.com/", 'User-Agent': geturls_ua
, 'Accept-Encoding': compression_supported
, 'Accept-Language': "en-US,en;q=0.8,en-CA,en-GB;q=0.6", 'Accept-Charset': "ISO-8859-1,ISO-8859-15,utf-8;q=0.7,*;q=0.7", 'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 'Connection': "close"}):
570 if isinstance(headers
, dict):
572 if(sys
.version
[0]=="2"):
573 for headkey
, headvalue
in headers
.iteritems():
574 returnval
.append(headkey
+": "+headvalue
);
575 if(sys
.version
[0]>="3"):
576 for headkey
, headvalue
in headers
.items():
577 returnval
.append(headkey
+": "+headvalue
);
578 elif isinstance(headers
, list):
584 def make_http_headers_from_pycurl_to_dict(headers
):
586 headers
= headers
.strip().split('\r\n');
587 for header
in headers
:
588 parts
= header
.split(': ', 1)
591 header_dict
[key
.title()] = value
;
594 def make_http_headers_from_list_to_dict(headers
=[("Referer", "http://google.com/"), ("User-Agent", geturls_ua
), ("Accept-Encoding", compression_supported
), ("Accept-Language", "en-US,en;q=0.8,en-CA,en-GB;q=0.6"), ("Accept-Charset", "ISO-8859-1,ISO-8859-15,utf-8;q=0.7,*;q=0.7"), ("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"), ("Connection", "close")]):
595 if isinstance(headers
, list):
600 returnval
.update({headers
[mli
][0]: headers
[mli
][1]});
602 elif isinstance(headers
, dict):
608 def get_httplib_support(checkvalue
=None):
609 global haverequests
, havemechanize
, havehttplib2
, haveurllib3
, havehttpx
, havehttpcore
, haveparamiko
, havepysftp
;
611 returnval
.append("ftp");
612 returnval
.append("httplib");
614 returnval
.append("httplib2");
615 returnval
.append("urllib");
617 returnval
.append("urllib3");
618 returnval
.append("request3");
619 returnval
.append("request");
621 returnval
.append("requests");
623 returnval
.append("aiohttp");
625 returnval
.append("httpx");
626 returnval
.append("httpx2");
628 returnval
.append("mechanize");
630 returnval
.append("pycurl");
631 if(hasattr(pycurl
, "CURL_HTTP_VERSION_2_0")):
632 returnval
.append("pycurl2");
633 if(hasattr(pycurl
, "CURL_HTTP_VERSION_3_0")):
634 returnval
.append("pycurl3");
636 returnval
.append("sftp");
638 returnval
.append("pysftp");
639 if(not checkvalue
is None):
640 if(checkvalue
=="urllib1" or checkvalue
=="urllib2"):
641 checkvalue
= "urllib";
642 if(checkvalue
=="httplib1"):
643 checkvalue
= "httplib";
644 if(checkvalue
in returnval
):
650 def check_httplib_support(checkvalue
="urllib"):
651 if(checkvalue
=="urllib1" or checkvalue
=="urllib2"):
652 checkvalue
= "urllib";
653 if(checkvalue
=="httplib1"):
654 checkvalue
= "httplib";
655 returnval
= get_httplib_support(checkvalue
);
658 def get_httplib_support_list():
659 returnval
= get_httplib_support(None);
662 def download_from_url(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, httplibuse
="urllib", buffersize
=524288, sleep
=-1, timeout
=10):
663 global geturls_download_sleep
, havezstd
, havebrotli
, haveaiohttp
, haverequests
, havemechanize
, havepycurl
, havehttplib2
, haveurllib3
, havehttpx
, havehttpcore
, haveparamiko
, havepysftp
;
665 sleep
= geturls_download_sleep
;
668 if(httplibuse
=="urllib1" or httplibuse
=="urllib2" or httplibuse
=="request"):
669 httplibuse
= "urllib";
670 if(httplibuse
=="httplib1"):
671 httplibuse
= "httplib";
672 if(not haverequests
and httplibuse
=="requests"):
673 httplibuse
= "urllib";
674 if(not haveaiohttp
and httplibuse
=="aiohttp"):
675 httplibuse
= "urllib";
676 if(not havehttpx
and httplibuse
=="httpx"):
677 httplibuse
= "urllib";
678 if(not havehttpx
and httplibuse
=="httpx2"):
679 httplibuse
= "urllib";
680 if(not havehttpcore
and httplibuse
=="httpcore"):
681 httplibuse
= "urllib";
682 if(not havehttpcore
and httplibuse
=="httpcore2"):
683 httplibuse
= "urllib";
684 if(not havemechanize
and httplibuse
=="mechanize"):
685 httplibuse
= "urllib";
686 if(not havepycurl
and httplibuse
=="pycurl"):
687 httplibuse
= "urllib";
688 if(not havepycurl
and httplibuse
=="pycurl2"):
689 httplibuse
= "urllib";
690 if(havepycurl
and httplibuse
=="pycurl2" and not hasattr(pycurl
, "CURL_HTTP_VERSION_2_0")):
691 httplibuse
= "pycurl";
692 if(not havepycurl
and httplibuse
=="pycurl3"):
693 httplibuse
= "urllib";
694 if(havepycurl
and httplibuse
=="pycurl3" and not hasattr(pycurl
, "CURL_HTTP_VERSION_3_0") and hasattr(pycurl
, "CURL_HTTP_VERSION_2_0")):
695 httplibuse
= "pycurl2";
696 if(havepycurl
and httplibuse
=="pycurl3" and not hasattr(pycurl
, "CURL_HTTP_VERSION_3_0") and not hasattr(pycurl
, "CURL_HTTP_VERSION_2_0")):
697 httplibuse
= "pycurl";
698 if(not havehttplib2
and httplibuse
=="httplib2"):
699 httplibuse
= "httplib";
700 if(not haveparamiko
and httplibuse
=="sftp"):
702 if(not havepysftp
and httplibuse
=="pysftp"):
704 urlparts
= urlparse
.urlparse(httpurl
);
705 if(isinstance(httpheaders
, list)):
706 httpheaders
= make_http_headers_from_list_to_dict(httpheaders
);
707 httpheaders
= fix_header_names(httpheaders
);
708 if(httpuseragent
is not None):
709 if('User-Agent' in httpheaders
):
710 httpheaders
['User-Agent'] = httpuseragent
;
712 httpuseragent
.update({'User-Agent': httpuseragent
});
713 if(httpreferer
is not None):
714 if('Referer' in httpheaders
):
715 httpheaders
['Referer'] = httpreferer
;
717 httpuseragent
.update({'Referer': httpreferer
});
718 if(urlparts
.username
is not None or urlparts
.password
is not None):
719 if(sys
.version
[0]=="2"):
720 inurlencode
= b64encode(str(urlparts
.username
+":"+urlparts
.password
));
721 if(sys
.version
[0]>="3"):
722 inurlencode
= b64encode(str(urlparts
.username
+":"+urlparts
.password
).encode()).decode("UTF-8");
723 httpheaders
.update( { 'Authorization': "Basic "+inurlencode
} );
724 geturls_opener
= build_opener(HTTPCookieProcessor(httpcookie
));
725 if(httplibuse
=="urllib" or httplibuse
=="mechanize"):
726 if(isinstance(httpheaders
, dict)):
727 httpheaders
= make_http_headers_from_dict_to_list(httpheaders
);
728 if(httplibuse
=="pycurl" or httplibuse
=="pycurl2" or httplibuse
=="pycurl3"):
729 if(isinstance(httpheaders
, dict)):
730 httpheaders
= make_http_headers_from_dict_to_pycurl(httpheaders
);
731 geturls_opener
.addheaders
= httpheaders
;
733 if(postdata
is not None and not isinstance(postdata
, dict)):
734 postdata
= urlencode(postdata
);
735 if(httplibuse
=="urllib" or httplibuse
=="request"):
736 geturls_request
= Request(httpurl
);
738 if(httpmethod
=="GET"):
739 geturls_text
= geturls_opener
.open(geturls_request
);
740 elif(httpmethod
=="POST"):
741 geturls_text
= geturls_opener
.open(geturls_request
, data
=postdata
);
743 geturls_text
= geturls_opener
.open(geturls_request
);
744 except HTTPError
as geturls_text_error
:
745 geturls_text
= geturls_text_error
;
746 log
.info("Error With URL "+httpurl
);
748 log
.info("Error With URL "+httpurl
);
750 except socket
.timeout
:
751 log
.info("Error With URL "+httpurl
);
753 httpcodeout
= geturls_text
.getcode();
755 httpcodereason
= geturls_text
.reason
;
756 except AttributeError:
757 httpcodereason
= http_status_to_reason(geturls_text
.getcode());
759 httpversionout
= geturls_text
.version
;
760 except AttributeError:
761 httpversionout
= "1.1";
762 httpmethodout
= geturls_request
.get_method();
763 httpurlout
= geturls_text
.geturl();
764 httpheaderout
= geturls_text
.info();
765 httpheadersentout
= httpheaders
;
766 elif(httplibuse
=="httplib"):
767 if(urlparts
[0]=="http"):
768 httpconn
= HTTPConnection(urlparts
[1], timeout
=timeout
);
769 elif(urlparts
[0]=="https"):
770 httpconn
= HTTPSConnection(urlparts
[1], timeout
=timeout
);
773 if(postdata
is not None and not isinstance(postdata
, dict)):
774 postdata
= urlencode(postdata
);
776 if(httpmethod
=="GET"):
777 httpconn
.request("GET", urlparts
[2], headers
=httpheaders
);
778 elif(httpmethod
=="POST"):
779 httpconn
.request("GET", urlparts
[2], body
=postdata
, headers
=httpheaders
);
781 httpconn
.request("GET", urlparts
[2], headers
=httpheaders
);
782 except socket
.timeout
:
783 log
.info("Error With URL "+httpurl
);
785 except socket
.gaierror
:
786 log
.info("Error With URL "+httpurl
);
788 except BlockingIOError
:
789 log
.info("Error With URL "+httpurl
);
791 geturls_text
= httpconn
.getresponse();
792 httpcodeout
= geturls_text
.status
;
793 httpcodereason
= geturls_text
.reason
;
794 if(geturls_text
.version
=="10"):
795 httpversionout
= "1.0";
797 httpversionout
= "1.1";
798 httpmethodout
= geturls_text
._method
;
799 httpurlout
= httpurl
;
800 httpheaderout
= geturls_text
.getheaders();
801 httpheadersentout
= httpheaders
;
802 elif(httplibuse
=="httplib2"):
803 if(urlparts
[0]=="http"):
804 httpconn
= HTTPConnectionWithTimeout(urlparts
[1], timeout
=timeout
);
805 elif(urlparts
[0]=="https"):
806 httpconn
= HTTPSConnectionWithTimeout(urlparts
[1], timeout
=timeout
);
809 if(postdata
is not None and not isinstance(postdata
, dict)):
810 postdata
= urlencode(postdata
);
812 if(httpmethod
=="GET"):
813 httpconn
.request("GET", urlparts
[2], headers
=httpheaders
);
814 elif(httpmethod
=="POST"):
815 httpconn
.request("GET", urlparts
[2], body
=postdata
, headers
=httpheaders
);
817 httpconn
.request("GET", urlparts
[2], headers
=httpheaders
);
818 except socket
.timeout
:
819 log
.info("Error With URL "+httpurl
);
821 except socket
.gaierror
:
822 log
.info("Error With URL "+httpurl
);
824 except BlockingIOError
:
825 log
.info("Error With URL "+httpurl
);
827 geturls_text
= httpconn
.getresponse();
828 httpcodeout
= geturls_text
.status
;
829 httpcodereason
= geturls_text
.reason
;
830 if(geturls_text
.version
=="10"):
831 httpversionout
= "1.0";
833 httpversionout
= "1.1";
834 httpmethodout
= httpmethod
;
835 httpurlout
= httpurl
;
836 httpheaderout
= geturls_text
.getheaders();
837 httpheadersentout
= httpheaders
;
838 elif(httplibuse
=="urllib3" or httplibuse
=="request3"):
839 timeout
= urllib3
.util
.Timeout(connect
=timeout
, read
=timeout
);
840 urllib_pool
= urllib3
.PoolManager(headers
=httpheaders
, timeout
=timeout
);
842 if(httpmethod
=="GET"):
843 geturls_text
= urllib_pool
.request("GET", httpurl
, headers
=httpheaders
, preload_content
=False);
844 elif(httpmethod
=="POST"):
845 geturls_text
= urllib_pool
.request("POST", httpurl
, body
=postdata
, headers
=httpheaders
, preload_content
=False);
847 geturls_text
= urllib_pool
.request("GET", httpurl
, headers
=httpheaders
, preload_content
=False);
848 except urllib3
.exceptions
.ConnectTimeoutError
:
849 log
.info("Error With URL "+httpurl
);
851 except urllib3
.exceptions
.ConnectError
:
852 log
.info("Error With URL "+httpurl
);
854 except urllib3
.exceptions
.MaxRetryError
:
855 log
.info("Error With URL "+httpurl
);
857 except socket
.timeout
:
858 log
.info("Error With URL "+httpurl
);
861 log
.info("Error With URL "+httpurl
);
863 httpcodeout
= geturls_text
.status
;
864 httpcodereason
= geturls_text
.reason
;
865 if(geturls_text
.version
=="10"):
866 httpversionout
= "1.0";
868 httpversionout
= "1.1";
869 httpmethodout
= httpmethod
;
870 httpurlout
= geturls_text
.geturl();
871 httpheaderout
= geturls_text
.info();
872 httpheadersentout
= httpheaders
;
873 elif(httplibuse
=="requests"):
875 reqsession
= requests
.Session();
876 if(httpmethod
=="GET"):
877 geturls_text
= reqsession
.get(httpurl
, timeout
=timeout
, headers
=httpheaders
, cookies
=httpcookie
);
878 elif(httpmethod
=="POST"):
879 geturls_text
= reqsession
.post(httpurl
, timeout
=timeout
, data
=postdata
, headers
=httpheaders
, cookies
=httpcookie
);
881 geturls_text
= reqsession
.get(httpurl
, timeout
=timeout
, headers
=httpheaders
, cookies
=httpcookie
);
882 except requests
.exceptions
.ConnectTimeout
:
883 log
.info("Error With URL "+httpurl
);
885 except requests
.exceptions
.ConnectError
:
886 log
.info("Error With URL "+httpurl
);
888 except socket
.timeout
:
889 log
.info("Error With URL "+httpurl
);
891 httpcodeout
= geturls_text
.status_code
;
892 httpcodereason
= geturls_text
.reason
;
893 if(geturls_text
.raw
.version
=="10"):
894 httpversionout
= "1.0";
896 httpversionout
= "1.1";
897 httpmethodout
= httpmethod
;
898 httpurlout
= geturls_text
.url
;
899 httpheaderout
= geturls_text
.headers
;
900 httpheadersentout
= geturls_text
.request
.headers
;
901 elif(httplibuse
=="aiohttp"):
903 reqsession
= aiohttp
.ClientSession(cookie_jar
=httpcookie
, headers
=httpheaders
, timeout
=timeout
, read_timeout
=timeout
, conn_timeout
=timeout
, read_bufsize
=buffersize
);
904 if(httpmethod
=="GET"):
905 geturls_text
= reqsession
.get(httpurl
);
906 elif(httpmethod
=="POST"):
907 geturls_text
= reqsession
.post(httpurl
, data
=postdata
);
909 geturls_text
= reqsession
.get(httpurl
);
910 except aiohttp
.exceptions
.ConnectTimeout
:
911 log
.info("Error With URL "+httpurl
);
913 except aiohttp
.exceptions
.ConnectError
:
914 log
.info("Error With URL "+httpurl
);
916 except socket
.timeout
:
917 log
.info("Error With URL "+httpurl
);
919 httpcodeout
= geturls_text
.status
;
920 httpcodereason
= geturls_text
.reason
;
921 httpversionout
= geturls_text
.version
;
922 httpmethodout
= geturls_text
.method
;
923 httpurlout
= geturls_text
.url
;
924 httpheaderout
= geturls_text
.headers
;
925 httpheadersentout
= geturls_text
.request_info
.headers
;
926 elif(httplibuse
=="httpx"):
928 if(httpmethod
=="GET"):
929 httpx_pool
= httpx
.Client(http1
=True, http2
=False, trust_env
=True);
930 geturls_text
= httpx_pool
.get(httpurl
, timeout
=timeout
, headers
=httpheaders
, cookies
=httpcookie
);
931 elif(httpmethod
=="POST"):
932 httpx_pool
= httpx
.Client(http1
=True, http2
=False, trust_env
=True);
933 geturls_text
= httpx_pool
.post(httpurl
, timeout
=timeout
, data
=postdata
, headers
=httpheaders
, cookies
=httpcookie
);
935 httpx_pool
= httpx
.Client(http1
=True, http2
=False, trust_env
=True);
936 geturls_text
= httpx_pool
.get(httpurl
, timeout
=timeout
, headers
=httpheaders
, cookies
=httpcookie
);
937 except httpx
.ConnectTimeout
:
938 log
.info("Error With URL "+httpurl
);
940 except httpx
.ConnectError
:
941 log
.info("Error With URL "+httpurl
);
943 except socket
.timeout
:
944 log
.info("Error With URL "+httpurl
);
946 httpcodeout
= geturls_text
.status_code
;
948 httpcodereason
= geturls_text
.reason_phrase
;
950 httpcodereason
= http_status_to_reason(geturls_text
.status_code
);
951 httpversionout
= geturls_text
.http_version
;
952 httpmethodout
= httpmethod
;
953 httpurlout
= str(geturls_text
.url
);
954 httpheaderout
= geturls_text
.headers
;
955 httpheadersentout
= geturls_text
.request
.headers
;
956 elif(httplibuse
=="httpx2"):
958 if(httpmethod
=="GET"):
959 httpx_pool
= httpx
.Client(http1
=True, http2
=True, trust_env
=True);
960 geturls_text
= httpx_pool
.get(httpurl
, timeout
=timeout
, headers
=httpheaders
, cookies
=httpcookie
);
961 elif(httpmethod
=="POST"):
962 httpx_pool
= httpx
.Client(http1
=True, http2
=True, trust_env
=True);
963 geturls_text
= httpx_pool
.post(httpurl
, timeout
=timeout
, data
=postdata
, headers
=httpheaders
, cookies
=httpcookie
);
965 httpx_pool
= httpx
.Client(http1
=True, http2
=True, trust_env
=True);
966 geturls_text
= httpx_pool
.get(httpurl
, timeout
=timeout
, headers
=httpheaders
, cookies
=httpcookie
);
967 except httpx
.ConnectTimeout
:
968 log
.info("Error With URL "+httpurl
);
970 except httpx
.ConnectError
:
971 log
.info("Error With URL "+httpurl
);
973 except socket
.timeout
:
974 log
.info("Error With URL "+httpurl
);
976 httpcodeout
= geturls_text
.status_code
;
978 httpcodereason
= geturls_text
.reason_phrase
;
980 httpcodereason
= http_status_to_reason(geturls_text
.status_code
);
981 httpversionout
= geturls_text
.http_version
;
982 httpmethodout
= httpmethod
;
983 httpurlout
= str(geturls_text
.url
);
984 httpheaderout
= geturls_text
.headers
;
985 httpheadersentout
= geturls_text
.request
.headers
;
986 elif(httplibuse
=="httpcore"):
988 if(httpmethod
=="GET"):
989 httpx_pool
= httpcore
.ConnectionPool(http1
=True, http2
=False);
990 geturls_text
= httpx_pool
.request("GET", httpurl
, headers
=httpheaders
);
991 elif(httpmethod
=="POST"):
992 httpx_pool
= httpcore
.ConnectionPool(http1
=True, http2
=False);
993 geturls_text
= httpx_pool
.request("GET", httpurl
, data
=postdata
, headers
=httpheaders
);
995 httpx_pool
= httpcore
.ConnectionPool(http1
=True, http2
=False);
996 geturls_text
= httpx_pool
.request("GET", httpurl
, headers
=httpheaders
);
997 except httpcore
.ConnectTimeout
:
998 log
.info("Error With URL "+httpurl
);
1000 except httpcore
.ConnectError
:
1001 log
.info("Error With URL "+httpurl
);
1003 except socket
.timeout
:
1004 log
.info("Error With URL "+httpurl
);
1006 httpcodeout
= geturls_text
.status
;
1007 httpcodereason
= http_status_to_reason(geturls_text
.status
);
1008 httpversionout
= "1.1";
1009 httpmethodout
= httpmethod
;
1010 httpurlout
= str(httpurl
);
1011 httpheaderout
= geturls_text
.headers
;
1012 httpheadersentout
= httpheaders
;
1013 elif(httplibuse
=="httpcore2"):
1015 if(httpmethod
=="GET"):
1016 httpx_pool
= httpcore
.ConnectionPool(http1
=True, http2
=True);
1017 geturls_text
= httpx_pool
.request("GET", httpurl
, headers
=httpheaders
);
1018 elif(httpmethod
=="POST"):
1019 httpx_pool
= httpcore
.ConnectionPool(http1
=True, http2
=True);
1020 geturls_text
= httpx_pool
.request("GET", httpurl
, data
=postdata
, headers
=httpheaders
);
1022 httpx_pool
= httpcore
.ConnectionPool(http1
=True, http2
=True);
1023 geturls_text
= httpx_pool
.request("GET", httpurl
, headers
=httpheaders
);
1024 except httpcore
.ConnectTimeout
:
1025 log
.info("Error With URL "+httpurl
);
1027 except httpcore
.ConnectError
:
1028 log
.info("Error With URL "+httpurl
);
1030 except socket
.timeout
:
1031 log
.info("Error With URL "+httpurl
);
1033 httpcodeout
= geturls_text
.status
;
1034 httpcodereason
= http_status_to_reason(geturls_text
.status
);
1035 httpversionout
= "1.1";
1036 httpmethodout
= httpmethod
;
1037 httpurlout
= str(httpurl
);
1038 httpheaderout
= geturls_text
.headers
;
1039 httpheadersentout
= httpheaders
;
1040 elif(httplibuse
=="mechanize"):
1041 geturls_opener
= mechanize
.Browser();
1042 if(isinstance(httpheaders
, dict)):
1043 httpheaders
= make_http_headers_from_dict_to_list(httpheaders
);
1045 geturls_opener
.addheaders
= httpheaders
;
1046 geturls_opener
.set_cookiejar(httpcookie
);
1047 geturls_opener
.set_handle_robots(False);
1048 if(postdata
is not None and not isinstance(postdata
, dict)):
1049 postdata
= urlencode(postdata
);
1051 if(httpmethod
=="GET"):
1052 geturls_text
= geturls_opener
.open(httpurl
);
1053 elif(httpmethod
=="POST"):
1054 geturls_text
= geturls_opener
.open(httpurl
, data
=postdata
);
1056 geturls_text
= geturls_opener
.open(httpurl
);
1057 except mechanize
.HTTPError
as geturls_text_error
:
1058 geturls_text
= geturls_text_error
;
1059 log
.info("Error With URL "+httpurl
);
1061 log
.info("Error With URL "+httpurl
);
1063 except socket
.timeout
:
1064 log
.info("Error With URL "+httpurl
);
1066 httpcodeout
= geturls_text
.code
;
1067 httpcodereason
= geturls_text
.msg
;
1068 httpversionout
= "1.1";
1069 httpmethodout
= httpmethod
;
1070 httpurlout
= geturls_text
.geturl();
1071 httpheaderout
= geturls_text
.info();
1072 reqhead
= geturls_opener
.request
;
1073 httpheadersentout
= reqhead
.header_items();
1074 elif(httplibuse
=="pycurl"):
1075 retrieved_body
= BytesIO();
1076 retrieved_headers
= BytesIO();
1078 if(httpmethod
=="GET"):
1079 geturls_text
= pycurl
.Curl();
1080 geturls_text
.setopt(geturls_text
.URL
, httpurl
);
1081 geturls_text
.setopt(geturls_text
.HTTP_VERSION
, geturls_text
.CURL_HTTP_VERSION_1_1
);
1082 geturls_text
.setopt(geturls_text
.WRITEFUNCTION
, retrieved_body
.write
);
1083 geturls_text
.setopt(geturls_text
.HTTPHEADER
, httpheaders
);
1084 geturls_text
.setopt(geturls_text
.HEADERFUNCTION
, retrieved_headers
.write
);
1085 geturls_text
.setopt(geturls_text
.FOLLOWLOCATION
, True);
1086 geturls_text
.setopt(geturls_text
.TIMEOUT
, timeout
);
1087 geturls_text
.perform();
1088 elif(httpmethod
=="POST"):
1089 geturls_text
= pycurl
.Curl();
1090 geturls_text
.setopt(geturls_text
.URL
, httpurl
);
1091 geturls_text
.setopt(geturls_text
.HTTP_VERSION
, geturls_text
.CURL_HTTP_VERSION_1_1
);
1092 geturls_text
.setopt(geturls_text
.WRITEFUNCTION
, retrieved_body
.write
);
1093 geturls_text
.setopt(geturls_text
.HTTPHEADER
, httpheaders
);
1094 geturls_text
.setopt(geturls_text
.HEADERFUNCTION
, retrieved_headers
.write
);
1095 geturls_text
.setopt(geturls_text
.FOLLOWLOCATION
, True);
1096 geturls_text
.setopt(geturls_text
.TIMEOUT
, timeout
);
1097 geturls_text
.setopt(geturls_text
.POST
, True);
1098 geturls_text
.setopt(geturls_text
.POSTFIELDS
, postdata
);
1099 geturls_text
.perform();
1101 geturls_text
= pycurl
.Curl();
1102 geturls_text
.setopt(geturls_text
.URL
, httpurl
);
1103 geturls_text
.setopt(geturls_text
.HTTP_VERSION
, geturls_text
.CURL_HTTP_VERSION_1_1
);
1104 geturls_text
.setopt(geturls_text
.WRITEFUNCTION
, retrieved_body
.write
);
1105 geturls_text
.setopt(geturls_text
.HTTPHEADER
, httpheaders
);
1106 geturls_text
.setopt(geturls_text
.HEADERFUNCTION
, retrieved_headers
.write
);
1107 geturls_text
.setopt(geturls_text
.FOLLOWLOCATION
, True);
1108 geturls_text
.setopt(geturls_text
.TIMEOUT
, timeout
);
1109 geturls_text
.perform();
1110 retrieved_headers
.seek(0);
1111 if(sys
.version
[0]=="2"):
1112 pycurlhead
= retrieved_headers
.read();
1113 if(sys
.version
[0]>="3"):
1114 pycurlhead
= retrieved_headers
.read().decode('UTF-8');
1115 pyhttpverinfo
= re
.findall(r
'^HTTP/([0-9.]+) (\d+)(?: ([A-Za-z\s]+))?$', pycurlhead
.splitlines()[0].strip().rstrip('\r\n'))[0];
1116 pycurlheadersout
= make_http_headers_from_pycurl_to_dict(pycurlhead
);
1117 retrieved_body
.seek(0);
1118 except socket
.timeout
:
1119 log
.info("Error With URL "+httpurl
);
1121 except socket
.gaierror
:
1122 log
.info("Error With URL "+httpurl
);
1125 log
.info("Error With URL "+httpurl
);
1127 httpcodeout
= geturls_text
.getinfo(geturls_text
.HTTP_CODE
);
1128 httpcodereason
= http_status_to_reason(geturls_text
.getinfo(geturls_text
.HTTP_CODE
));
1129 httpversionout
= pyhttpverinfo
[0];
1130 httpmethodout
= httpmethod
;
1131 httpurlout
= geturls_text
.getinfo(geturls_text
.EFFECTIVE_URL
);
1132 httpheaderout
= pycurlheadersout
;
1133 httpheadersentout
= httpheaders
;
1134 elif(httplibuse
=="pycurl2"):
1135 retrieved_body
= BytesIO();
1136 retrieved_headers
= BytesIO();
1138 if(httpmethod
=="GET"):
1139 geturls_text
= pycurl
.Curl();
1140 geturls_text
.setopt(geturls_text
.HTTP_VERSION
, geturls_text
.CURL_HTTP_VERSION_2_0
);
1141 geturls_text
.setopt(geturls_text
.URL
, httpurl
);
1142 geturls_text
.setopt(geturls_text
.WRITEFUNCTION
, retrieved_body
.write
);
1143 geturls_text
.setopt(geturls_text
.HTTPHEADER
, httpheaders
);
1144 geturls_text
.setopt(geturls_text
.HEADERFUNCTION
, retrieved_headers
.write
);
1145 geturls_text
.setopt(geturls_text
.FOLLOWLOCATION
, True);
1146 geturls_text
.setopt(geturls_text
.TIMEOUT
, timeout
);
1147 geturls_text
.perform();
1148 elif(httpmethod
=="POST"):
1149 geturls_text
= pycurl
.Curl();
1150 geturls_text
.setopt(geturls_text
.HTTP_VERSION
, geturls_text
.CURL_HTTP_VERSION_2_0
);
1151 geturls_text
.setopt(geturls_text
.URL
, httpurl
);
1152 geturls_text
.setopt(geturls_text
.WRITEFUNCTION
, retrieved_body
.write
);
1153 geturls_text
.setopt(geturls_text
.HTTPHEADER
, httpheaders
);
1154 geturls_text
.setopt(geturls_text
.HEADERFUNCTION
, retrieved_headers
.write
);
1155 geturls_text
.setopt(geturls_text
.FOLLOWLOCATION
, True);
1156 geturls_text
.setopt(geturls_text
.TIMEOUT
, timeout
);
1157 geturls_text
.setopt(geturls_text
.POST
, True);
1158 geturls_text
.setopt(geturls_text
.POSTFIELDS
, postdata
);
1159 geturls_text
.perform();
1161 geturls_text
= pycurl
.Curl();
1162 geturls_text
.setopt(geturls_text
.HTTP_VERSION
, geturls_text
.CURL_HTTP_VERSION_2_0
);
1163 geturls_text
.setopt(geturls_text
.URL
, httpurl
);
1164 geturls_text
.setopt(geturls_text
.WRITEFUNCTION
, retrieved_body
.write
);
1165 geturls_text
.setopt(geturls_text
.HTTPHEADER
, httpheaders
);
1166 geturls_text
.setopt(geturls_text
.HEADERFUNCTION
, retrieved_headers
.write
);
1167 geturls_text
.setopt(geturls_text
.FOLLOWLOCATION
, True);
1168 geturls_text
.setopt(geturls_text
.TIMEOUT
, timeout
);
1169 geturls_text
.perform();
1170 retrieved_headers
.seek(0);
1171 if(sys
.version
[0]=="2"):
1172 pycurlhead
= retrieved_headers
.read();
1173 if(sys
.version
[0]>="3"):
1174 pycurlhead
= retrieved_headers
.read().decode('UTF-8');
1175 pyhttpverinfo
= re
.findall(r
'^HTTP/([0-9.]+) (\d+)(?: ([A-Za-z\s]+))?$', pycurlhead
.splitlines()[0].strip())[0];
1176 pycurlheadersout
= make_http_headers_from_pycurl_to_dict(pycurlhead
);
1177 retrieved_body
.seek(0);
1178 except socket
.timeout
:
1179 log
.info("Error With URL "+httpurl
);
1181 except socket
.gaierror
:
1182 log
.info("Error With URL "+httpurl
);
1185 log
.info("Error With URL "+httpurl
);
1187 httpcodeout
= geturls_text
.getinfo(geturls_text
.HTTP_CODE
);
1188 httpcodereason
= http_status_to_reason(geturls_text
.getinfo(geturls_text
.HTTP_CODE
));
1189 httpversionout
= pyhttpverinfo
[0];
1190 httpmethodout
= httpmethod
;
1191 httpurlout
= geturls_text
.getinfo(geturls_text
.EFFECTIVE_URL
);
1192 httpheaderout
= pycurlheadersout
;
1193 httpheadersentout
= httpheaders
;
1194 elif(httplibuse
=="pycurl3"):
1195 retrieved_body
= BytesIO();
1196 retrieved_headers
= BytesIO();
1198 if(httpmethod
=="GET"):
1199 geturls_text
= pycurl
.Curl();
1200 geturls_text
.setopt(geturls_text
.HTTP_VERSION
, geturls_text
.CURL_HTTP_VERSION_3_0
);
1201 geturls_text
.setopt(geturls_text
.URL
, httpurl
);
1202 geturls_text
.setopt(geturls_text
.WRITEFUNCTION
, retrieved_body
.write
);
1203 geturls_text
.setopt(geturls_text
.HTTPHEADER
, httpheaders
);
1204 geturls_text
.setopt(geturls_text
.HEADERFUNCTION
, retrieved_headers
.write
);
1205 geturls_text
.setopt(geturls_text
.FOLLOWLOCATION
, True);
1206 geturls_text
.setopt(geturls_text
.TIMEOUT
, timeout
);
1207 geturls_text
.perform();
1208 elif(httpmethod
=="POST"):
1209 geturls_text
= pycurl
.Curl();
1210 geturls_text
.setopt(geturls_text
.HTTP_VERSION
, geturls_text
.CURL_HTTP_VERSION_3_0
);
1211 geturls_text
.setopt(geturls_text
.URL
, httpurl
);
1212 geturls_text
.setopt(geturls_text
.WRITEFUNCTION
, retrieved_body
.write
);
1213 geturls_text
.setopt(geturls_text
.HTTPHEADER
, httpheaders
);
1214 geturls_text
.setopt(geturls_text
.HEADERFUNCTION
, retrieved_headers
.write
);
1215 geturls_text
.setopt(geturls_text
.FOLLOWLOCATION
, True);
1216 geturls_text
.setopt(geturls_text
.TIMEOUT
, timeout
);
1217 geturls_text
.setopt(geturls_text
.POST
, True);
1218 geturls_text
.setopt(geturls_text
.POSTFIELDS
, postdata
);
1219 geturls_text
.perform();
1221 geturls_text
= pycurl
.Curl();
1222 geturls_text
.setopt(geturls_text
.HTTP_VERSION
, geturls_text
.CURL_HTTP_VERSION_3_0
);
1223 geturls_text
.setopt(geturls_text
.URL
, httpurl
);
1224 geturls_text
.setopt(geturls_text
.WRITEFUNCTION
, retrieved_body
.write
);
1225 geturls_text
.setopt(geturls_text
.HTTPHEADER
, httpheaders
);
1226 geturls_text
.setopt(geturls_text
.HEADERFUNCTION
, retrieved_headers
.write
);
1227 geturls_text
.setopt(geturls_text
.FOLLOWLOCATION
, True);
1228 geturls_text
.setopt(geturls_text
.TIMEOUT
, timeout
);
1229 geturls_text
.perform();
1230 retrieved_headers
.seek(0);
1231 if(sys
.version
[0]=="2"):
1232 pycurlhead
= retrieved_headers
.read();
1233 if(sys
.version
[0]>="3"):
1234 pycurlhead
= retrieved_headers
.read().decode('UTF-8');
1235 pyhttpverinfo
= re
.findall(r
'^HTTP/([0-9.]+) (\d+)(?: ([A-Za-z\s]+))?$', pycurlhead
.splitlines()[0].strip().rstrip('\r\n'))[0];
1236 pycurlheadersout
= make_http_headers_from_pycurl_to_dict(pycurlhead
);
1237 retrieved_body
.seek(0);
1238 except socket
.timeout
:
1239 log
.info("Error With URL "+httpurl
);
1241 except socket
.gaierror
:
1242 log
.info("Error With URL "+httpurl
);
1245 log
.info("Error With URL "+httpurl
);
1247 httpcodeout
= geturls_text
.getinfo(geturls_text
.HTTP_CODE
);
1248 httpcodereason
= http_status_to_reason(geturls_text
.getinfo(geturls_text
.HTTP_CODE
));
1249 httpversionout
= pyhttpverinfo
[0];
1250 httpmethodout
= httpmethod
;
1251 httpurlout
= geturls_text
.getinfo(geturls_text
.EFFECTIVE_URL
);
1252 httpheaderout
= pycurlheadersout
;
1253 httpheadersentout
= httpheaders
;
1254 elif(httplibuse
=="ftp"):
1255 geturls_text
= download_file_from_ftp_file(httpurl
);
1256 if(not geturls_text
):
1258 downloadsize
= None;
1259 if(downloadsize
is not None):
1260 downloadsize
= int(downloadsize
);
1261 if downloadsize
is None: downloadsize
= 0;
1264 log
.info("Downloading URL "+httpurl
);
1265 with
BytesIO() as strbuf
:
1267 databytes
= geturls_text
.read(buffersize
);
1268 if not databytes
: break;
1269 datasize
= len(databytes
);
1270 fulldatasize
= datasize
+ fulldatasize
;
1273 percentage
= str("{0:.2f}".format(float(float(fulldatasize
/ downloadsize
) * 100))).rstrip('0').rstrip('.')+"%";
1274 downloaddiff
= fulldatasize
- prevdownsize
;
1275 log
.info("Downloading "+get_readable_size(fulldatasize
, 2, "SI")['ReadableWithSuffix']+" / "+get_readable_size(downloadsize
, 2, "SI")['ReadableWithSuffix']+" "+str(percentage
)+" / Downloaded "+get_readable_size(downloaddiff
, 2, "IEC")['ReadableWithSuffix']);
1276 prevdownsize
= fulldatasize
;
1277 strbuf
.write(databytes
);
1279 returnval_content
= strbuf
.read();
1280 returnval
= {'Type': "Content", 'Content': returnval_content
, 'Contentsize': fulldatasize
, 'ContentsizeAlt': {'IEC': get_readable_size(fulldatasize
, 2, "IEC"), 'SI': get_readable_size(fulldatasize
, 2, "SI")}, 'Headers': None, 'Version': None, 'Method': None, 'HeadersSent': None, 'URL': httpurl
, 'Code': None};
1281 geturls_text
.close();
1282 elif(httplibuse
=="sftp"):
1283 geturls_text
= download_file_from_sftp_file(httpurl
);
1284 if(not geturls_text
):
1286 downloadsize
= None;
1287 if(downloadsize
is not None):
1288 downloadsize
= int(downloadsize
);
1289 if downloadsize
is None: downloadsize
= 0;
1292 log
.info("Downloading URL "+httpurl
);
1293 with
BytesIO() as strbuf
:
1295 databytes
= geturls_text
.read(buffersize
);
1296 if not databytes
: break;
1297 datasize
= len(databytes
);
1298 fulldatasize
= datasize
+ fulldatasize
;
1301 percentage
= str("{0:.2f}".format(float(float(fulldatasize
/ downloadsize
) * 100))).rstrip('0').rstrip('.')+"%";
1302 downloaddiff
= fulldatasize
- prevdownsize
;
1303 log
.info("Downloading "+get_readable_size(fulldatasize
, 2, "SI")['ReadableWithSuffix']+" / "+get_readable_size(downloadsize
, 2, "SI")['ReadableWithSuffix']+" "+str(percentage
)+" / Downloaded "+get_readable_size(downloaddiff
, 2, "IEC")['ReadableWithSuffix']);
1304 prevdownsize
= fulldatasize
;
1305 strbuf
.write(databytes
);
1307 returnval_content
= strbuf
.read();
1308 returnval
= {'Type': "Content", 'Content': returnval_content
, 'Contentsize': fulldatasize
, 'ContentsizeAlt': {'IEC': get_readable_size(fulldatasize
, 2, "IEC"), 'SI': get_readable_size(fulldatasize
, 2, "SI")}, 'Headers': None, 'Version': None, 'Method': None, 'HeadersSent': None, 'URL': httpurl
, 'Code': None};
1309 geturls_text
.close();
1311 elif(httplibuse
=="pysftp"):
1312 geturls_text
= download_file_from_pysftp_file(httpurl
);
1313 if(not geturls_text
):
1315 downloadsize
= None;
1316 if(downloadsize
is not None):
1317 downloadsize
= int(downloadsize
);
1318 if downloadsize
is None: downloadsize
= 0;
1321 log
.info("Downloading URL "+httpurl
);
1322 with
BytesIO() as strbuf
:
1324 databytes
= geturls_text
.read(buffersize
);
1325 if not databytes
: break;
1326 datasize
= len(databytes
);
1327 fulldatasize
= datasize
+ fulldatasize
;
1330 percentage
= str("{0:.2f}".format(float(float(fulldatasize
/ downloadsize
) * 100))).rstrip('0').rstrip('.')+"%";
1331 downloaddiff
= fulldatasize
- prevdownsize
;
1332 log
.info("Downloading "+get_readable_size(fulldatasize
, 2, "SI")['ReadableWithSuffix']+" / "+get_readable_size(downloadsize
, 2, "SI")['ReadableWithSuffix']+" "+str(percentage
)+" / Downloaded "+get_readable_size(downloaddiff
, 2, "IEC")['ReadableWithSuffix']);
1333 prevdownsize
= fulldatasize
;
1334 strbuf
.write(databytes
);
1336 returnval_content
= strbuf
.read();
1337 returnval
= {'Type': "Content", 'Content': returnval_content
, 'Contentsize': fulldatasize
, 'ContentsizeAlt': {'IEC': get_readable_size(fulldatasize
, 2, "IEC"), 'SI': get_readable_size(fulldatasize
, 2, "SI")}, 'Headers': None, 'Version': None, 'Method': None, 'HeadersSent': None, 'URL': httpurl
, 'Code': None};
1338 geturls_text
.close();
1342 if(isinstance(httpheaderout
, list) and (httplibuse
!="pycurl" and httplibuse
!="pycurl2" and httplibuse
!="pycurl3")):
1343 httpheaderout
= dict(make_http_headers_from_list_to_dict(httpheaderout
));
1344 if(isinstance(httpheaderout
, list) and (httplibuse
=="pycurl" or httplibuse
=="pycurl2" or httplibuse
=="pycurl3")):
1345 httpheaderout
= dict(make_http_headers_from_pycurl_to_dict("\r\n".join(httpheaderout
)));
1346 if(sys
.version
[0]=="2"):
1348 prehttpheaderout
= httpheaderout
;
1349 httpheaderkeys
= httpheaderout
.keys();
1350 imax
= len(httpheaderkeys
);
1354 httpheaderout
.update({httpheaderkeys
[ic
]: prehttpheaderout
[httpheaderkeys
[ic
]]});
1356 except AttributeError:
1358 httpheaderout
= fix_header_names(httpheaderout
);
1359 if(isinstance(httpheadersentout
, list) and (httplibuse
!="pycurl" and httplibuse
!="pycurl2" and httplibuse
!="pycurl3")):
1360 httpheadersentout
= dict(make_http_headers_from_list_to_dict(httpheadersentout
));
1361 if(isinstance(httpheadersentout
, list) and (httplibuse
=="pycurl" or httplibuse
=="pycurl2" or httplibuse
=="pycurl3")):
1362 httpheadersentout
= dict(make_http_headers_from_pycurl_to_dict("\r\n".join(httpheadersentout
)));
1363 httpheadersentout
= fix_header_names(httpheadersentout
);
1364 log
.info("Downloading URL "+httpurl
);
1365 if(httplibuse
=="urllib" or httplibuse
=="request" or httplibuse
=="request3" or httplibuse
=="aiohttp" or httplibuse
=="httplib" or httplibuse
=="httplib2" or httplibuse
=="urllib3" or httplibuse
=="mechanize"):
1366 downloadsize
= httpheaderout
.get('Content-Length');
1367 if(downloadsize
is not None):
1368 downloadsize
= int(downloadsize
);
1369 if downloadsize
is None: downloadsize
= 0;
1372 log
.info("Downloading URL "+httpurl
);
1373 with
BytesIO() as strbuf
:
1375 databytes
= geturls_text
.read(buffersize
);
1376 if not databytes
: break;
1377 datasize
= len(databytes
);
1378 fulldatasize
= datasize
+ fulldatasize
;
1381 percentage
= str("{0:.2f}".format(float(float(fulldatasize
/ downloadsize
) * 100))).rstrip('0').rstrip('.')+"%";
1382 downloaddiff
= fulldatasize
- prevdownsize
;
1383 log
.info("Downloading "+get_readable_size(fulldatasize
, 2, "SI")['ReadableWithSuffix']+" / "+get_readable_size(downloadsize
, 2, "SI")['ReadableWithSuffix']+" "+str(percentage
)+" / Downloaded "+get_readable_size(downloaddiff
, 2, "IEC")['ReadableWithSuffix']);
1384 prevdownsize
= fulldatasize
;
1385 strbuf
.write(databytes
);
1387 returnval_content
= strbuf
.read();
1388 geturls_text
.close();
1389 if(httpheaderout
.get("Content-Encoding")=="gzip"):
1391 returnval_content
= zlib
.decompress(returnval_content
, 16+zlib
.MAX_WBITS
);
1394 elif(httpheaderout
.get("Content-Encoding")=="deflate"):
1396 returnval_content
= zlib
.decompress(returnval_content
);
1399 elif(httpheaderout
.get("Content-Encoding")=="br" and havebrotli
):
1401 returnval_content
= brotli
.decompress(returnval_content
);
1402 except brotli
.error
:
1404 elif(httpheaderout
.get("Content-Encoding")=="zstd" and havezstd
):
1406 returnval_content
= zstandard
.decompress(returnval_content
);
1407 except zstandard
.error
:
1409 elif((httpheaderout
.get("Content-Encoding")=="lzma" or httpheaderout
.get("Content-Encoding")=="xz") and havelzma
):
1411 returnval_content
= lzma
.decompress(returnval_content
);
1412 except zstandard
.error
:
1414 elif(httpheaderout
.get("Content-Encoding")=="bzip2"):
1416 returnval_content
= bz2
.decompress(returnval_content
);
1417 except zstandard
.error
:
1419 elif(httplibuse
=="httpx" or httplibuse
=="httpx2" or httplibuse
=="httpcore" or httplibuse
=="httpcore2"):
1420 downloadsize
= httpheaderout
.get('Content-Length');
1421 if(downloadsize
is not None):
1422 downloadsize
= int(downloadsize
);
1423 if downloadsize
is None: downloadsize
= 0;
1426 log
.info("Downloading URL "+httpurl
);
1427 with
BytesIO() as strbuf
:
1429 databytes
= geturls_text
.read();
1430 if not databytes
: break;
1431 datasize
= len(databytes
);
1432 fulldatasize
= datasize
+ fulldatasize
;
1435 percentage
= str("{0:.2f}".format(float(float(fulldatasize
/ downloadsize
) * 100))).rstrip('0').rstrip('.')+"%";
1436 downloaddiff
= fulldatasize
- prevdownsize
;
1437 log
.info("Downloading "+get_readable_size(fulldatasize
, 2, "SI")['ReadableWithSuffix']+" / "+get_readable_size(downloadsize
, 2, "SI")['ReadableWithSuffix']+" "+str(percentage
)+" / Downloaded "+get_readable_size(downloaddiff
, 2, "IEC")['ReadableWithSuffix']);
1438 prevdownsize
= fulldatasize
;
1439 strbuf
.write(databytes
);
1442 returnval_content
= strbuf
.read();
1443 geturls_text
.close();
1444 if(httpheaderout
.get("Content-Encoding")=="gzip"):
1446 returnval_content
= zlib
.decompress(returnval_content
, 16+zlib
.MAX_WBITS
);
1449 elif(httpheaderout
.get("Content-Encoding")=="deflate"):
1451 returnval_content
= zlib
.decompress(returnval_content
);
1454 elif(httpheaderout
.get("Content-Encoding")=="br" and havebrotli
):
1456 returnval_content
= brotli
.decompress(returnval_content
);
1457 except brotli
.error
:
1459 elif(httpheaderout
.get("Content-Encoding")=="zstd" and havezstd
):
1461 returnval_content
= zstandard
.decompress(returnval_content
);
1462 except zstandard
.error
:
1464 elif((httpheaderout
.get("Content-Encoding")=="lzma" or httpheaderout
.get("Content-Encoding")=="xz") and havelzma
):
1466 returnval_content
= lzma
.decompress(returnval_content
);
1467 except zstandard
.error
:
1469 elif(httpheaderout
.get("Content-Encoding")=="bzip2"):
1471 returnval_content
= bz2
.decompress(returnval_content
);
1472 except zstandard
.error
:
1474 elif(httplibuse
=="requests"):
1475 log
.info("Downloading URL "+httpurl
);
1476 downloadsize
= httpheaderout
.get('Content-Length');
1477 if(downloadsize
is not None):
1478 downloadsize
= int(downloadsize
);
1479 if downloadsize
is None: downloadsize
= 0;
1482 log
.info("Downloading URL "+httpurl
);
1483 with
BytesIO() as strbuf
:
1485 databytes
= geturls_text
.raw
.read(buffersize
);
1486 if not databytes
: break;
1487 datasize
= len(databytes
);
1488 fulldatasize
= datasize
+ fulldatasize
;
1491 percentage
= str("{0:.2f}".format(float(float(fulldatasize
/ downloadsize
) * 100))).rstrip('0').rstrip('.')+"%";
1492 downloaddiff
= fulldatasize
- prevdownsize
;
1493 log
.info("Downloading "+get_readable_size(fulldatasize
, 2, "SI")['ReadableWithSuffix']+" / "+get_readable_size(downloadsize
, 2, "SI")['ReadableWithSuffix']+" "+str(percentage
)+" / Downloaded "+get_readable_size(downloaddiff
, 2, "IEC")['ReadableWithSuffix']);
1494 prevdownsize
= fulldatasize
;
1495 strbuf
.write(databytes
);
1497 returnval_content
= strbuf
.read();
1498 geturls_text
.close();
1499 if(httpheaderout
.get("Content-Encoding")=="gzip"):
1501 returnval_content
= zlib
.decompress(returnval_content
, 16+zlib
.MAX_WBITS
);
1504 elif(httpheaderout
.get("Content-Encoding")=="deflate"):
1506 returnval_content
= zlib
.decompress(returnval_content
);
1509 elif(httpheaderout
.get("Content-Encoding")=="br" and havebrotli
):
1511 returnval_content
= brotli
.decompress(returnval_content
);
1512 except brotli
.error
:
1514 elif(httpheaderout
.get("Content-Encoding")=="zstd" and havezstd
):
1516 returnval_content
= zstandard
.decompress(returnval_content
);
1517 except zstandard
.error
:
1519 elif((httpheaderout
.get("Content-Encoding")=="lzma" or httpheaderout
.get("Content-Encoding")=="xz") and havelzma
):
1521 returnval_content
= lzma
.decompress(returnval_content
);
1522 except zstandard
.error
:
1524 elif(httpheaderout
.get("Content-Encoding")=="bzip2"):
1526 returnval_content
= bz2
.decompress(returnval_content
);
1527 except zstandard
.error
:
1529 elif(httplibuse
=="pycurl" or httplibuse
=="pycurl2" or httplibuse
=="pycurl3"):
1530 log
.info("Downloading URL "+httpurl
);
1531 downloadsize
= httpheaderout
.get('Content-Length');
1532 if(downloadsize
is not None):
1533 downloadsize
= int(downloadsize
);
1534 if downloadsize
is None: downloadsize
= 0;
1537 log
.info("Downloading URL "+httpurl
);
1538 with
BytesIO() as strbuf
:
1540 databytes
= retrieved_body
.read(buffersize
);
1541 if not databytes
: break;
1542 datasize
= len(databytes
);
1543 fulldatasize
= datasize
+ fulldatasize
;
1546 percentage
= str("{0:.2f}".format(float(float(fulldatasize
/ downloadsize
) * 100))).rstrip('0').rstrip('.')+"%";
1547 downloaddiff
= fulldatasize
- prevdownsize
;
1548 log
.info("Downloading "+get_readable_size(fulldatasize
, 2, "SI")['ReadableWithSuffix']+" / "+get_readable_size(downloadsize
, 2, "SI")['ReadableWithSuffix']+" "+str(percentage
)+" / Downloaded "+get_readable_size(downloaddiff
, 2, "IEC")['ReadableWithSuffix']);
1549 prevdownsize
= fulldatasize
;
1550 strbuf
.write(databytes
);
1552 returnval_content
= strbuf
.read();
1553 geturls_text
.close();
1554 if(httpheaderout
.get("Content-Encoding")=="gzip"):
1556 returnval_content
= zlib
.decompress(returnval_content
, 16+zlib
.MAX_WBITS
);
1559 elif(httpheaderout
.get("Content-Encoding")=="deflate"):
1561 returnval_content
= zlib
.decompress(returnval_content
);
1564 elif(httpheaderout
.get("Content-Encoding")=="br" and havebrotli
):
1566 returnval_content
= brotli
.decompress(returnval_content
);
1567 except brotli
.error
:
1569 elif(httpheaderout
.get("Content-Encoding")=="zstd" and havezstd
):
1571 returnval_content
= zstandard
.decompress(returnval_content
);
1572 except zstandard
.error
:
1574 elif((httpheaderout
.get("Content-Encoding")=="lzma" or httpheaderout
.get("Content-Encoding")=="xz") and havelzma
):
1576 returnval_content
= lzma
.decompress(returnval_content
);
1577 except zstandard
.error
:
1579 elif(httpheaderout
.get("Content-Encoding")=="bzip2"):
1581 returnval_content
= bz2
.decompress(returnval_content
);
1582 except zstandard
.error
:
1584 elif(httplibuse
=="ftp" or httplibuse
=="sftp" or httplibuse
=="pysftp"):
1588 returnval
= {'Type': "Content", 'Content': returnval_content
, 'Contentsize': fulldatasize
, 'ContentsizeAlt': {'IEC': get_readable_size(fulldatasize
, 2, "IEC"), 'SI': get_readable_size(fulldatasize
, 2, "SI")}, 'Headers': httpheaderout
, 'Version': httpversionout
, 'Method': httpmethodout
, 'HeadersSent': httpheadersentout
, 'URL': httpurlout
, 'Code': httpcodeout
, 'Reason': httpcodereason
, 'HTTPLib': httplibuse
};
1591 def download_from_url_from_list(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, httplibuse
="urllib", buffersize
=524288, sleep
=-1, timeout
=10):
1592 if(isinstance(httpurl
, list)):
1594 elif(isinstance(httpurl
, tuple)):
1596 elif(isinstance(httpurl
, dict)):
1597 httpurl
= httpurl
.values();
1599 httpurl
= [httpurl
];
1600 listsize
= len(httpurl
);
1603 while(listcount
<listsize
):
1604 ouputval
= download_from_url(httpurl
[listcount
], httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, httplibuse
, buffersize
, sleep
, timeout
);
1605 returnval
.append(ouputval
);
1609 def download_from_url_file(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, httplibuse
="urllib", ranges
=[None, None], buffersize
=524288, sleep
=-1, timeout
=10):
1610 global geturls_download_sleep
, havezstd
, havebrotli
, tmpfileprefix
, tmpfilesuffix
, haveaiohttp
, haverequests
, havemechanize
, havehttplib2
, haveurllib3
, havehttpx
, havehttpcore
, haveparamiko
, havepysftp
;
1611 exec_time_start
= time
.time();
1612 myhash
= hashlib
.new("sha1");
1613 if(sys
.version
[0]=="2"):
1614 myhash
.update(httpurl
);
1615 myhash
.update(str(buffersize
));
1616 myhash
.update(str(exec_time_start
));
1617 if(sys
.version
[0]>="3"):
1618 myhash
.update(httpurl
.encode('utf-8'));
1619 myhash
.update(str(buffersize
).encode('utf-8'));
1620 myhash
.update(str(exec_time_start
).encode('utf-8'));
1621 newtmpfilesuffix
= tmpfilesuffix
+ str(myhash
.hexdigest());
1623 sleep
= geturls_download_sleep
;
1626 if(httplibuse
=="urllib1" or httplibuse
=="urllib2" or httplibuse
=="request"):
1627 httplibuse
= "urllib";
1628 if(httplibuse
=="httplib1"):
1629 httplibuse
= "httplib";
1630 if(not haverequests
and httplibuse
=="requests"):
1631 httplibuse
= "urllib";
1632 if(not haveaiohttp
and httplibuse
=="aiohttp"):
1633 httplibuse
= "urllib";
1634 if(not havehttpx
and httplibuse
=="httpx"):
1635 httplibuse
= "urllib";
1636 if(not havehttpx
and httplibuse
=="httpx2"):
1637 httplibuse
= "urllib";
1638 if(not havehttpcore
and httplibuse
=="httpcore"):
1639 httplibuse
= "urllib";
1640 if(not havehttpcore
and httplibuse
=="httpcore2"):
1641 httplibuse
= "urllib";
1642 if(not havemechanize
and httplibuse
=="mechanize"):
1643 httplibuse
= "urllib";
1644 if(not havepycurl
and httplibuse
=="pycurl"):
1645 httplibuse
= "urllib";
1646 if(not havepycurl
and httplibuse
=="pycurl2"):
1647 httplibuse
= "urllib";
1648 if(havepycurl
and httplibuse
=="pycurl2" and not hasattr(pycurl
, "CURL_HTTP_VERSION_2_0")):
1649 httplibuse
= "pycurl";
1650 if(not havepycurl
and httplibuse
=="pycurl3"):
1651 httplibuse
= "urllib";
1652 if(havepycurl
and httplibuse
=="pycurl3" and not hasattr(pycurl
, "CURL_HTTP_VERSION_3_0") and hasattr(pycurl
, "CURL_HTTP_VERSION_2_0")):
1653 httplibuse
= "pycurl2";
1654 if(havepycurl
and httplibuse
=="pycurl3" and not hasattr(pycurl
, "CURL_HTTP_VERSION_3_0") and not hasattr(pycurl
, "CURL_HTTP_VERSION_2_0")):
1655 httplibuse
= "pycurl";
1656 if(not havehttplib2
and httplibuse
=="httplib2"):
1657 httplibuse
= "httplib";
1658 if(not haveparamiko
and httplibuse
=="sftp"):
1660 if(not haveparamiko
and httplibuse
=="pysftp"):
1662 pretmpfilename
= download_from_url(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, httplibuse
, buffersize
, sleep
, timeout
);
1663 if(not pretmpfilename
):
1665 with tempfile
.NamedTemporaryFile('wb+', prefix
=tmpfileprefix
, suffix
=newtmpfilesuffix
, delete
=False) as f
:
1666 tmpfilename
= f
.name
;
1668 os
.utime(tmpfilename
, (time
.mktime(email
.utils
.parsedate_to_datetime(pretmpfilename
.get('Headers').get('Last-Modified')).timetuple()), time
.mktime(email
.utils
.parsedate_to_datetime(pretmpfilename
.get('Headers').get('Last-Modified')).timetuple())));
1669 except AttributeError:
1671 os
.utime(tmpfilename
, (time
.mktime(datetime
.datetime
.strptime(pretmpfilename
.get('Headers').get('Last-Modified'), "%a, %d %b %Y %H:%M:%S %Z").timetuple()), time
.mktime(datetime
.datetime
.strptime(pretmpfilename
.get('Headers').get('Last-Modified'), "%a, %d %b %Y %H:%M:%S %Z").timetuple())));
1676 returnval
= {'Type': "File", 'Filename': tmpfilename
, 'Filesize': pretmpfilename
.get('Contentsize'), 'FilesizeAlt': {'IEC': get_readable_size(pretmpfilename
.get('Contentsize'), 2, "IEC"), 'SI': get_readable_size(pretmpfilename
.get('Contentsize'), 2, "SI")}, 'Headers': pretmpfilename
.get('Headers'), 'Version': pretmpfilename
.get('Version'), 'Method': pretmpfilename
.get('Method'), 'HeadersSent': pretmpfilename
.get('HeadersSent'), 'URL': pretmpfilename
.get('URL'), 'Code': pretmpfilename
.get('Code'), 'Reason': pretmpfilename
.get('Reason'), 'HTTPLib': pretmpfilename
.get('HTTPLib')};
1677 f
.write(pretmpfilename
.get('Content'));
1679 exec_time_end
= time
.time();
1680 log
.info("It took "+hms_string(exec_time_start
- exec_time_end
)+" to download file.");
1681 returnval
.update({'Filesize': os
.path
.getsize(tmpfilename
), 'FilesizeAlt': {'IEC': get_readable_size(os
.path
.getsize(tmpfilename
), 2, "IEC"), 'SI': get_readable_size(os
.path
.getsize(tmpfilename
), 2, "SI")}, 'DownloadTime': float(exec_time_start
- exec_time_end
), 'DownloadTimeReadable': hms_string(exec_time_start
- exec_time_end
)});
1684 def download_from_url_file_with_list(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, httplibuse
="urllib", ranges
=[None, None], buffersize
=524288, sleep
=-1, timeout
=10):
1685 if(isinstance(httpurl
, list)):
1687 elif(isinstance(httpurl
, tuple)):
1689 elif(isinstance(httpurl
, dict)):
1690 httpurl
= httpurl
.values();
1692 httpurl
= [httpurl
];
1693 listsize
= len(httpurl
);
1696 while(listcount
<listsize
):
1697 ouputval
= download_from_url_file(httpurl
[listcount
], httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, httplibuse
, ranges
, buffersize
, sleep
, timeout
);
1698 returnval
.append(ouputval
);
1702 def download_from_url_to_file(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, httplibuse
="urllib", outfile
="-", outpath
=os
.getcwd(), ranges
=[None, None], buffersize
=[524288, 524288], sleep
=-1, timeout
=10):
1703 global geturls_download_sleep
, havezstd
, havebrotli
, haveaiohttp
, haverequests
, havemechanize
, havepycurl
, havehttplib2
, haveurllib3
, havehttpx
, havehttpcore
, haveparamiko
, havepysftp
;
1705 sleep
= geturls_download_sleep
;
1708 if(httplibuse
=="urllib1" or httplibuse
=="urllib2" or httplibuse
=="request"):
1709 httplibuse
= "urllib";
1710 if(httplibuse
=="httplib1"):
1711 httplibuse
= "httplib";
1712 if(not haverequests
and httplibuse
=="requests"):
1713 httplibuse
= "urllib";
1714 if(not haveaiohttp
and httplibuse
=="aiohttp"):
1715 httplibuse
= "urllib";
1716 if(not havehttpx
and httplibuse
=="httpx"):
1717 httplibuse
= "urllib";
1718 if(not havehttpx
and httplibuse
=="httpx2"):
1719 httplibuse
= "urllib";
1720 if(not havehttpcore
and httplibuse
=="httpcore"):
1721 httplibuse
= "urllib";
1722 if(not havehttpcore
and httplibuse
=="httpcore2"):
1723 httplibuse
= "urllib";
1724 if(not havemechanize
and httplibuse
=="mechanize"):
1725 httplibuse
= "urllib";
1726 if(not havepycurl
and httplibuse
=="pycurl"):
1727 httplibuse
= "urllib";
1728 if(not havepycurl
and httplibuse
=="pycurl2"):
1729 httplibuse
= "urllib";
1730 if(havepycurl
and httplibuse
=="pycurl2" and not hasattr(pycurl
, "CURL_HTTP_VERSION_2_0")):
1731 httplibuse
= "pycurl";
1732 if(not havepycurl
and httplibuse
=="pycurl3"):
1733 httplibuse
= "urllib";
1734 if(havepycurl
and httplibuse
=="pycurl3" and not hasattr(pycurl
, "CURL_HTTP_VERSION_3_0") and hasattr(pycurl
, "CURL_HTTP_VERSION_2_0")):
1735 httplibuse
= "pycurl2";
1736 if(havepycurl
and httplibuse
=="pycurl3" and not hasattr(pycurl
, "CURL_HTTP_VERSION_3_0") and not hasattr(pycurl
, "CURL_HTTP_VERSION_2_0")):
1737 httplibuse
= "pycurl";
1738 if(not havehttplib2
and httplibuse
=="httplib2"):
1739 httplibuse
= "httplib";
1740 if(not haveparamiko
and httplibuse
=="sftp"):
1742 if(not havepysftp
and httplibuse
=="pysftp"):
1744 if(not outfile
=="-"):
1745 outpath
= outpath
.rstrip(os
.path
.sep
);
1746 filepath
= os
.path
.realpath(outpath
+os
.path
.sep
+outfile
);
1747 if(not os
.path
.exists(outpath
)):
1748 os
.makedirs(outpath
);
1749 if(os
.path
.exists(outpath
) and os
.path
.isfile(outpath
)):
1751 if(os
.path
.exists(filepath
) and os
.path
.isdir(filepath
)):
1753 pretmpfilename
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, httplibuse
, ranges
, buffersize
[0], sleep
, timeout
);
1754 if(not pretmpfilename
):
1756 tmpfilename
= pretmpfilename
.get('Filename');
1757 downloadsize
= int(os
.path
.getsize(tmpfilename
));
1759 log
.info("Moving file "+tmpfilename
+" to "+filepath
);
1760 exec_time_start
= time
.time();
1761 shutil
.move(tmpfilename
, filepath
);
1763 os
.utime(filepath
, (time
.mktime(email
.utils
.parsedate_to_datetime(pretmpfilename
.get('Headers').get('Last-Modified')).timetuple()), time
.mktime(email
.utils
.parsedate_to_datetime(pretmpfilename
.get('Headers').get('Last-Modified')).timetuple())));
1764 except AttributeError:
1766 os
.utime(filepath
, (time
.mktime(datetime
.datetime
.strptime(pretmpfilename
.get('Headers').get('Last-Modified'), "%a, %d %b %Y %H:%M:%S %Z").timetuple()), time
.mktime(datetime
.datetime
.strptime(pretmpfilename
.get('Headers').get('Last-Modified'), "%a, %d %b %Y %H:%M:%S %Z").timetuple())));
1771 exec_time_end
= time
.time();
1772 log
.info("It took "+hms_string(exec_time_start
- exec_time_end
)+" to move file.");
1773 if(os
.path
.exists(tmpfilename
)):
1774 os
.remove(tmpfilename
);
1775 returnval
= {'Type': "File", 'Filename': filepath
, 'Filesize': downloadsize
, 'FilesizeAlt': {'IEC': get_readable_size(downloadsize
, 2, "IEC"), 'SI': get_readable_size(downloadsize
, 2, "SI")}, 'DownloadTime': pretmpfilename
.get('DownloadTime'), 'DownloadTimeReadable': pretmpfilename
.get('DownloadTimeReadable'), 'MoveFileTime': float(exec_time_start
- exec_time_end
), 'MoveFileTimeReadable': hms_string(exec_time_start
- exec_time_end
), 'Headers': pretmpfilename
.get('Headers'), 'Version': pretmpfilename
.get('Version'), 'Method': pretmpfilename
.get('Method'), 'Method': httpmethod
, 'HeadersSent': pretmpfilename
.get('HeadersSent'), 'URL': pretmpfilename
.get('URL'), 'Code': pretmpfilename
.get('Code'), 'Reason': pretmpfilename
.get('Reason'), 'HTTPLib': pretmpfilename
.get('HTTPLib')};
1777 pretmpfilename
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, httplibuse
, ranges
, buffersize
[0], sleep
, timeout
);
1778 tmpfilename
= pretmpfilename
.get('Filename');
1779 downloadsize
= int(os
.path
.getsize(tmpfilename
));
1782 exec_time_start
= time
.time();
1783 with
open(tmpfilename
, 'rb') as ft
:
1786 databytes
= ft
.read(buffersize
[1]);
1787 if not databytes
: break;
1788 datasize
= len(databytes
);
1789 fulldatasize
= datasize
+ fulldatasize
;
1792 percentage
= str("{0:.2f}".format(float(float(fulldatasize
/ downloadsize
) * 100))).rstrip('0').rstrip('.')+"%";
1793 downloaddiff
= fulldatasize
- prevdownsize
;
1794 log
.info("Copying "+get_readable_size(fulldatasize
, 2, "SI")['ReadableWithSuffix']+" / "+get_readable_size(downloadsize
, 2, "SI")['ReadableWithSuffix']+" "+str(percentage
)+" / Copied "+get_readable_size(downloaddiff
, 2, "IEC")['ReadableWithSuffix']);
1795 prevdownsize
= fulldatasize
;
1798 fdata
= f
.getvalue();
1801 os
.remove(tmpfilename
);
1802 exec_time_end
= time
.time();
1803 log
.info("It took "+hms_string(exec_time_start
- exec_time_end
)+" to copy file.");
1804 returnval
= {'Type': "Content", 'Content': fdata
, 'Contentsize': downloadsize
, 'ContentsizeAlt': {'IEC': get_readable_size(downloadsize
, 2, "IEC"), 'SI': get_readable_size(downloadsize
, 2, "SI")}, 'DownloadTime': pretmpfilename
.get('DownloadTime'), 'DownloadTimeReadable': pretmpfilename
.get('DownloadTimeReadable'), 'MoveFileTime': float(exec_time_start
- exec_time_end
), 'MoveFileTimeReadable': hms_string(exec_time_start
- exec_time_end
), 'Headers': pretmpfilename
.get('Headers'), 'Version': pretmpfilename
.get('Version'), 'Method': pretmpfilename
.get('Method'), 'Method': httpmethod
, 'HeadersSent': pretmpfilename
.get('HeadersSent'), 'URL': pretmpfilename
.get('URL'), 'Code': pretmpfilename
.get('Code'), 'Reason': pretmpfilename
.get('Reason'), 'HTTPLib': httplibuse
};
1807 def download_from_url_to_file_with_list(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, httplibuse
="urllib", outfile
="-", outpath
=os
.getcwd(), ranges
=[None, None], buffersize
=[524288, 524288], sleep
=-1, timeout
=10):
1808 if(isinstance(httpurl
, list)):
1810 elif(isinstance(httpurl
, tuple)):
1812 elif(isinstance(httpurl
, dict)):
1813 httpurl
= httpurl
.values();
1815 httpurl
= [httpurl
];
1816 listsize
= len(httpurl
);
1819 while(listcount
<listsize
):
1820 ouputval
= download_from_url_to_file(httpurl
[listcount
], httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, httplibuse
, outfile
, outpath
, ranges
, buffersize
, sleep
, timeout
);
1821 returnval
.append(ouputval
);
1825 def download_from_url_with_urllib(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, buffersize
=524288, sleep
=-1, timeout
=10):
1826 returnval
= download_from_url(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "urllib", buffersize
, sleep
, timeout
);
1829 def download_from_url_with_request(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, buffersize
=524288, sleep
=-1, timeout
=10):
1830 returnval
= download_from_url(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "urllib", buffersize
, sleep
, timeout
);
1833 def download_from_url_with_request3(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, buffersize
=524288, sleep
=-1, timeout
=10):
1834 returnval
= download_from_url(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "request3", buffersize
, sleep
, timeout
);
1837 def download_from_url_with_httplib(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, buffersize
=524288, sleep
=-1, timeout
=10):
1838 returnval
= download_from_url(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "httplib", buffersize
, sleep
, timeout
);
1841 def download_from_url_with_httplib2(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, buffersize
=524288, sleep
=-1, timeout
=10):
1842 returnval
= download_from_url(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "httplib2", buffersize
, sleep
, timeout
);
1845 def download_from_url_with_urllib3(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, buffersize
=524288, sleep
=-1, timeout
=10):
1846 returnval
= download_from_url(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "urllib3", buffersize
, sleep
, timeout
);
1849 def download_from_url_with_requests(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, buffersize
=524288, sleep
=-1, timeout
=10):
1850 returnval
= download_from_url(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "requests", buffersize
, sleep
, timeout
);
1853 def download_from_url_with_aiohttp(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, buffersize
=524288, sleep
=-1, timeout
=10):
1854 returnval
= download_from_url(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "aiohttp", buffersize
, sleep
, timeout
);
1857 def download_from_url_with_httpx(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, buffersize
=524288, sleep
=-1, timeout
=10):
1858 returnval
= download_from_url(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "httpx", buffersize
, sleep
, timeout
);
1861 def download_from_url_with_httpx2(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, buffersize
=524288, sleep
=-1, timeout
=10):
1862 returnval
= download_from_url(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "httpx2", buffersize
, sleep
, timeout
);
1865 def download_from_url_with_httpcore(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, buffersize
=524288, sleep
=-1, timeout
=10):
1866 returnval
= download_from_url(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "httpcore", buffersize
, sleep
, timeout
);
1869 def download_from_url_with_httpcore2(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, buffersize
=524288, sleep
=-1, timeout
=10):
1870 returnval
= download_from_url(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "httpcore2", buffersize
, sleep
, timeout
);
1873 def download_from_url_with_mechanize(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, buffersize
=524288, sleep
=-1, timeout
=10):
1874 returnval
= download_from_url(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "mechanize", buffersize
, sleep
, timeout
);
1877 def download_from_url_with_pycurl(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, buffersize
=524288, sleep
=-1, timeout
=10):
1878 returnval
= download_from_url(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "pycurl", buffersize
, sleep
, timeout
);
1881 def download_from_url_with_pycurl2(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, buffersize
=524288, sleep
=-1, timeout
=10):
1882 returnval
= download_from_url(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "pycurl2", buffersize
, sleep
, timeout
);
1885 def download_from_url_with_pycurl3(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, buffersize
=524288, sleep
=-1, timeout
=10):
1886 returnval
= download_from_url(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "pycurl3", buffersize
, sleep
, timeout
);
1889 def download_from_url_with_ftp(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, buffersize
=524288, sleep
=-1, timeout
=10):
1890 returnval
= download_from_url(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "ftp", buffersize
, sleep
, timeout
);
1893 def download_from_url_with_sftp(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, buffersize
=524288, sleep
=-1, timeout
=10):
1894 returnval
= download_from_url(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "sftp", buffersize
, sleep
, timeout
);
1897 def download_from_url_with_pysftp(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, buffersize
=524288, sleep
=-1, timeout
=10):
1898 returnval
= download_from_url(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "pysftp", buffersize
, sleep
, timeout
);
1901 def download_from_url_file_with_urllib(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, ranges
=[None, None], buffersize
=524288, sleep
=-1, timeout
=10):
1902 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "urllib", ranges
, buffersize
, sleep
, timeout
);
1905 def download_from_url_file_with_request(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, ranges
=[None, None], buffersize
=524288, sleep
=-1, timeout
=10):
1906 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "urllib", ranges
, buffersize
, sleep
, timeout
);
1909 def download_from_url_file_with_request3(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, ranges
=[None, None], buffersize
=524288, sleep
=-1, timeout
=10):
1910 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "request3", ranges
, buffersize
, sleep
, timeout
);
1913 def download_from_url_file_with_httplib(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, ranges
=[None, None], buffersize
=524288, sleep
=-1, timeout
=10):
1914 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "httplib", ranges
, buffersize
, sleep
, timeout
);
1917 def download_from_url_file_with_httplib2(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, ranges
=[None, None], buffersize
=524288, sleep
=-1, timeout
=10):
1918 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "httplib2", ranges
, buffersize
, sleep
, timeout
);
1921 def download_from_url_file_with_urllib3(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, ranges
=[None, None], buffersize
=524288, sleep
=-1, timeout
=10):
1922 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "urllib3", ranges
, buffersize
, sleep
, timeout
);
1925 def download_from_url_file_with_requests(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, ranges
=[None, None], buffersize
=524288, sleep
=-1, timeout
=10):
1926 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "requests", ranges
, buffersize
, sleep
, timeout
);
1929 def download_from_url_file_with_aiohttp(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, ranges
=[None, None], buffersize
=524288, sleep
=-1, timeout
=10):
1930 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "aiohttp", ranges
, buffersize
, sleep
, timeout
);
1933 def download_from_url_file_with_httpx(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, ranges
=[None, None], buffersize
=524288, sleep
=-1, timeout
=10):
1934 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "httpx", ranges
, buffersize
, sleep
, timeout
);
1937 def download_from_url_file_with_httpx2(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, ranges
=[None, None], buffersize
=524288, sleep
=-1, timeout
=10):
1938 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "httpx2", ranges
, buffersize
, sleep
, timeout
);
1941 def download_from_url_file_with_httpcore(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, ranges
=[None, None], buffersize
=524288, sleep
=-1, timeout
=10):
1942 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "httpcore", ranges
, buffersize
, sleep
, timeout
);
1945 def download_from_url_file_with_httpcore2(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, ranges
=[None, None], buffersize
=524288, sleep
=-1, timeout
=10):
1946 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "httpcore2", ranges
, buffersize
, sleep
, timeout
);
1949 def download_from_url_file_with_mechanize(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, ranges
=[None, None], buffersize
=524288, sleep
=-1, timeout
=10):
1950 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "mechanize", ranges
, buffersize
, sleep
, timeout
);
1953 def download_from_url_file_with_pycurl(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, ranges
=[None, None], buffersize
=524288, sleep
=-1, timeout
=10):
1954 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "pycurl", ranges
, buffersize
, sleep
, timeout
);
1957 def download_from_url_file_with_pycurl2(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, ranges
=[None, None], buffersize
=524288, sleep
=-1, timeout
=10):
1958 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "pycurl2", ranges
, buffersize
, sleep
, timeout
);
1961 def download_from_url_file_with_pycurl3(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, ranges
=[None, None], buffersize
=524288, sleep
=-1, timeout
=10):
1962 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "pycurl3", ranges
, buffersize
, sleep
, timeout
);
1965 def download_from_url_file_with_ftp(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, ranges
=[None, None], buffersize
=524288, sleep
=-1, timeout
=10):
1966 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "ftp", ranges
, buffersize
, sleep
, timeout
);
1969 def download_from_url_file_with_sftp(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, ranges
=[None, None], buffersize
=524288, sleep
=-1, timeout
=10):
1970 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "sftp", ranges
, buffersize
, sleep
, timeout
);
1973 def download_from_url_file_with_pysftp(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, ranges
=[None, None], buffersize
=524288, sleep
=-1, timeout
=10):
1974 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "pysftp", ranges
, buffersize
, sleep
, timeout
);
1977 def download_from_url_to_file_with_urllib(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, outfile
="-", outpath
=os
.getcwd(), ranges
=[None, None], buffersize
=[524288, 524288], sleep
=-1, timeout
=10):
1978 returnval
= download_from_url_to_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "urllib", outfile
, outpath
, ranges
, buffersize
, sleep
, timeout
);
1981 def download_from_url_to_file_with_request(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, outfile
="-", outpath
=os
.getcwd(), ranges
=[None, None], buffersize
=[524288, 524288], sleep
=-1, timeout
=10):
1982 returnval
= download_from_url_to_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "request", outfile
, outpath
, ranges
, buffersize
, sleep
, timeout
);
1985 def download_from_url_to_file_with_request3(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, outfile
="-", outpath
=os
.getcwd(), ranges
=[None, None], buffersize
=[524288, 524288], sleep
=-1, timeout
=10):
1986 returnval
= download_from_url_to_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "urllib", outfile
, outpath
, ranges
, buffersize
, sleep
, timeout
);
1989 def download_from_url_to_file_with_httplib(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, outfile
="-", outpath
=os
.getcwd(), ranges
=[None, None], buffersize
=[524288, 524288], sleep
=-1, timeout
=10):
1990 returnval
= download_from_url_to_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "httplib", outfile
, outpath
, ranges
, buffersize
, sleep
, timeout
);
1993 def download_from_url_to_file_with_httplib2(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, outfile
="-", outpath
=os
.getcwd(), ranges
=[None, None], buffersize
=[524288, 524288], sleep
=-1, timeout
=10):
1994 returnval
= download_from_url_to_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "httplib2", outfile
, outpath
, ranges
, buffersize
, sleep
, timeout
);
1997 def download_from_url_to_file_with_urllib3(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, outfile
="-", outpath
=os
.getcwd(), ranges
=[None, None], buffersize
=[524288, 524288], sleep
=-1, timeout
=10):
1998 returnval
= download_from_url_to_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "urllib3", outfile
, outpath
, ranges
, buffersize
, sleep
, timeout
);
2001 def download_from_url_to_file_with_requests(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, outfile
="-", outpath
=os
.getcwd(), ranges
=[None, None], buffersize
=[524288, 524288], sleep
=-1, timeout
=10):
2002 returnval
= download_from_url_to_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "requests", outfile
, outpath
, ranges
, buffersize
, sleep
, timeout
);
2005 def download_from_url_to_file_with_aiohttp(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, outfile
="-", outpath
=os
.getcwd(), ranges
=[None, None], buffersize
=[524288, 524288], sleep
=-1, timeout
=10):
2006 returnval
= download_from_url_to_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "aiohttp", outfile
, outpath
, ranges
, buffersize
, sleep
, timeout
);
2009 def download_from_url_to_file_with_httpx(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, outfile
="-", outpath
=os
.getcwd(), ranges
=[None, None], buffersize
=[524288, 524288], sleep
=-1, timeout
=10):
2010 returnval
= download_from_url_to_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "httpx", outfile
, outpath
, ranges
, buffersize
, sleep
, timeout
);
2013 def download_from_url_to_file_with_httpx2(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, outfile
="-", outpath
=os
.getcwd(), ranges
=[None, None], buffersize
=[524288, 524288], sleep
=-1, timeout
=10):
2014 returnval
= download_from_url_to_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "httpx2", outfile
, outpath
, ranges
, buffersize
, sleep
, timeout
);
2017 def download_from_url_to_file_with_httpcore(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, outfile
="-", outpath
=os
.getcwd(), ranges
=[None, None], buffersize
=[524288, 524288], sleep
=-1, timeout
=10):
2018 returnval
= download_from_url_to_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "httpcore", outfile
, outpath
, ranges
, buffersize
, sleep
, timeout
);
2021 def download_from_url_to_file_with_httpcore2(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, outfile
="-", outpath
=os
.getcwd(), ranges
=[None, None], buffersize
=[524288, 524288], sleep
=-1, timeout
=10):
2022 returnval
= download_from_url_to_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "httpcore2", outfile
, outpath
, ranges
, buffersize
, sleep
, timeout
);
2025 def download_from_url_to_file_with_mechanize(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, outfile
="-", outpath
=os
.getcwd(), ranges
=[None, None], buffersize
=[524288, 524288], sleep
=-1, timeout
=10):
2026 returnval
= download_from_url_to_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "mechanize", outfile
, outpath
, ranges
, buffersize
, sleep
, timeout
);
2029 def download_from_url_to_file_with_pycurl(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, outfile
="-", outpath
=os
.getcwd(), ranges
=[None, None], buffersize
=[524288, 524288], sleep
=-1, timeout
=10):
2030 returnval
= download_from_url_to_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "pycurl", outfile
, outpath
, ranges
, buffersize
, sleep
, timeout
);
2033 def download_from_url_to_file_with_pycurl2(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, outfile
="-", outpath
=os
.getcwd(), ranges
=[None, None], buffersize
=[524288, 524288], sleep
=-1, timeout
=10):
2034 returnval
= download_from_url_to_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "pycurl2", outfile
, outpath
, ranges
, buffersize
, sleep
, timeout
);
2037 def download_from_url_to_file_with_pycurl3(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, outfile
="-", outpath
=os
.getcwd(), ranges
=[None, None], buffersize
=[524288, 524288], sleep
=-1, timeout
=10):
2038 returnval
= download_from_url_to_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "pycurl3", outfile
, outpath
, ranges
, buffersize
, sleep
, timeout
);
2041 def download_from_url_to_file_with_ftp(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, outfile
="-", outpath
=os
.getcwd(), ranges
=[None, None], buffersize
=[524288, 524288], sleep
=-1, timeout
=10):
2042 returnval
= download_from_url_to_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "ftp", outfile
, outpath
, ranges
, buffersize
, sleep
, timeout
);
2045 def download_from_url_to_file_with_sftp(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, outfile
="-", outpath
=os
.getcwd(), ranges
=[None, None], buffersize
=[524288, 524288], sleep
=-1, timeout
=10):
2046 returnval
= download_from_url_to_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "sftp", outfile
, outpath
, ranges
, buffersize
, sleep
, timeout
);
2049 def download_from_url_to_file_with_pysftp(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, outfile
="-", outpath
=os
.getcwd(), ranges
=[None, None], buffersize
=[524288, 524288], sleep
=-1, timeout
=10):
2050 returnval
= download_from_url_to_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "pysftp", outfile
, outpath
, ranges
, buffersize
, sleep
, timeout
);
2053 def download_file_from_ftp_file(url
):
2054 urlparts
= urlparse
.urlparse(url
);
2055 file_name
= os
.path
.basename(urlparts
.path
);
2056 file_dir
= os
.path
.dirname(urlparts
.path
);
2057 if(urlparts
.username
is not None):
2058 ftp_username
= urlparts
.username
;
2060 ftp_username
= "anonymous";
2061 if(urlparts
.password
is not None):
2062 ftp_password
= urlparts
.password
;
2063 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
2064 ftp_password
= "anonymous";
2067 if(urlparts
.scheme
=="ftp"):
2069 elif(urlparts
.scheme
=="ftps"):
2073 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
2075 ftp_port
= urlparts
.port
;
2076 if(urlparts
.port
is None):
2079 ftp
.connect(urlparts
.hostname
, ftp_port
);
2080 except socket
.gaierror
:
2081 log
.info("Error With URL "+httpurl
);
2083 except socket
.timeout
:
2084 log
.info("Error With URL "+httpurl
);
2086 ftp
.login(urlparts
.username
, urlparts
.password
);
2087 if(urlparts
.scheme
=="ftps"):
2089 ftpfile
= BytesIO();
2090 ftp
.retrbinary("RETR "+urlparts
.path
, ftpfile
.write
);
2091 #ftp.storbinary("STOR "+urlparts.path, ftpfile.write);
2096 def download_file_from_ftp_string(url
):
2097 ftpfile
= download_file_from_ftp_file(url
);
2098 return ftpfile
.read();
2100 def upload_file_to_ftp_file(ftpfile
, url
):
2101 urlparts
= urlparse
.urlparse(url
);
2102 file_name
= os
.path
.basename(urlparts
.path
);
2103 file_dir
= os
.path
.dirname(urlparts
.path
);
2104 if(urlparts
.username
is not None):
2105 ftp_username
= urlparts
.username
;
2107 ftp_username
= "anonymous";
2108 if(urlparts
.password
is not None):
2109 ftp_password
= urlparts
.password
;
2110 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
2111 ftp_password
= "anonymous";
2114 if(urlparts
.scheme
=="ftp"):
2116 elif(urlparts
.scheme
=="ftps"):
2120 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
2122 ftp_port
= urlparts
.port
;
2123 if(urlparts
.port
is None):
2126 ftp
.connect(urlparts
.hostname
, ftp_port
);
2127 except socket
.gaierror
:
2128 log
.info("Error With URL "+httpurl
);
2130 except socket
.timeout
:
2131 log
.info("Error With URL "+httpurl
);
2133 ftp
.login(urlparts
.username
, urlparts
.password
);
2134 if(urlparts
.scheme
=="ftps"):
2136 ftp
.storbinary("STOR "+urlparts
.path
, ftpfile
);
2141 def upload_file_to_ftp_string(ftpstring
, url
):
2142 ftpfileo
= BytesIO(ftpstring
);
2143 ftpfile
= upload_file_to_ftp_file(ftpfileo
, url
);
2148 def download_file_from_sftp_file(url
):
2149 urlparts
= urlparse
.urlparse(url
);
2150 file_name
= os
.path
.basename(urlparts
.path
);
2151 file_dir
= os
.path
.dirname(urlparts
.path
);
2152 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
2154 sftp_port
= urlparts
.port
;
2155 if(urlparts
.port
is None):
2158 sftp_port
= urlparts
.port
;
2159 if(urlparts
.username
is not None):
2160 sftp_username
= urlparts
.username
;
2162 sftp_username
= "anonymous";
2163 if(urlparts
.password
is not None):
2164 sftp_password
= urlparts
.password
;
2165 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
2166 sftp_password
= "anonymous";
2169 if(urlparts
.scheme
!="sftp"):
2171 ssh
= paramiko
.SSHClient();
2172 ssh
.load_system_host_keys();
2173 ssh
.set_missing_host_key_policy(paramiko
.AutoAddPolicy());
2175 ssh
.connect(urlparts
.hostname
, port
=sftp_port
, username
=urlparts
.username
, password
=urlparts
.password
);
2176 except paramiko
.ssh_exception
.SSHException
:
2178 except socket
.gaierror
:
2179 log
.info("Error With URL "+httpurl
);
2181 except socket
.timeout
:
2182 log
.info("Error With URL "+httpurl
);
2184 sftp
= ssh
.open_sftp();
2185 sftpfile
= BytesIO();
2186 sftp
.getfo(urlparts
.path
, sftpfile
);
2189 sftpfile
.seek(0, 0);
2192 def download_file_from_sftp_file(url
):
2196 def download_file_from_sftp_string(url
):
2197 sftpfile
= download_file_from_sftp_file(url
);
2198 return sftpfile
.read();
2200 def download_file_from_ftp_string(url
):
2204 def upload_file_to_sftp_file(sftpfile
, url
):
2205 urlparts
= urlparse
.urlparse(url
);
2206 file_name
= os
.path
.basename(urlparts
.path
);
2207 file_dir
= os
.path
.dirname(urlparts
.path
);
2208 sftp_port
= urlparts
.port
;
2209 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
2211 if(urlparts
.port
is None):
2214 sftp_port
= urlparts
.port
;
2215 if(urlparts
.username
is not None):
2216 sftp_username
= urlparts
.username
;
2218 sftp_username
= "anonymous";
2219 if(urlparts
.password
is not None):
2220 sftp_password
= urlparts
.password
;
2221 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
2222 sftp_password
= "anonymous";
2225 if(urlparts
.scheme
!="sftp"):
2227 ssh
= paramiko
.SSHClient();
2228 ssh
.load_system_host_keys();
2229 ssh
.set_missing_host_key_policy(paramiko
.AutoAddPolicy());
2231 ssh
.connect(urlparts
.hostname
, port
=sftp_port
, username
=urlparts
.username
, password
=urlparts
.password
);
2232 except paramiko
.ssh_exception
.SSHException
:
2234 except socket
.gaierror
:
2235 log
.info("Error With URL "+httpurl
);
2237 except socket
.timeout
:
2238 log
.info("Error With URL "+httpurl
);
2240 sftp
= ssh
.open_sftp();
2241 sftp
.putfo(sftpfile
, urlparts
.path
);
2244 sftpfile
.seek(0, 0);
2247 def upload_file_to_sftp_file(sftpfile
, url
):
2251 def upload_file_to_sftp_string(sftpstring
, url
):
2252 sftpfileo
= BytesIO(sftpstring
);
2253 sftpfile
= upload_file_to_sftp_files(ftpfileo
, url
);
2257 def upload_file_to_sftp_string(url
):
2262 def download_file_from_pysftp_file(url
):
2263 urlparts
= urlparse
.urlparse(url
);
2264 file_name
= os
.path
.basename(urlparts
.path
);
2265 file_dir
= os
.path
.dirname(urlparts
.path
);
2266 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
2268 sftp_port
= urlparts
.port
;
2269 if(urlparts
.port
is None):
2272 sftp_port
= urlparts
.port
;
2273 if(urlparts
.username
is not None):
2274 sftp_username
= urlparts
.username
;
2276 sftp_username
= "anonymous";
2277 if(urlparts
.password
is not None):
2278 sftp_password
= urlparts
.password
;
2279 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
2280 sftp_password
= "anonymous";
2283 if(urlparts
.scheme
!="sftp"):
2286 pysftp
.Connection(urlparts
.hostname
, port
=sftp_port
, username
=urlparts
.username
, password
=urlparts
.password
);
2287 except paramiko
.ssh_exception
.SSHException
:
2289 except socket
.gaierror
:
2290 log
.info("Error With URL "+httpurl
);
2292 except socket
.timeout
:
2293 log
.info("Error With URL "+httpurl
);
2295 sftp
= ssh
.open_sftp();
2296 sftpfile
= BytesIO();
2297 sftp
.getfo(urlparts
.path
, sftpfile
);
2300 sftpfile
.seek(0, 0);
2303 def download_file_from_pysftp_file(url
):
2307 def download_file_from_pysftp_string(url
):
2308 sftpfile
= download_file_from_pysftp_file(url
);
2309 return sftpfile
.read();
2311 def download_file_from_ftp_string(url
):
2315 def upload_file_to_pysftp_file(sftpfile
, url
):
2316 urlparts
= urlparse
.urlparse(url
);
2317 file_name
= os
.path
.basename(urlparts
.path
);
2318 file_dir
= os
.path
.dirname(urlparts
.path
);
2319 sftp_port
= urlparts
.port
;
2320 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
2322 if(urlparts
.port
is None):
2325 sftp_port
= urlparts
.port
;
2326 if(urlparts
.username
is not None):
2327 sftp_username
= urlparts
.username
;
2329 sftp_username
= "anonymous";
2330 if(urlparts
.password
is not None):
2331 sftp_password
= urlparts
.password
;
2332 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
2333 sftp_password
= "anonymous";
2336 if(urlparts
.scheme
!="sftp"):
2339 pysftp
.Connection(urlparts
.hostname
, port
=sftp_port
, username
=urlparts
.username
, password
=urlparts
.password
);
2340 except paramiko
.ssh_exception
.SSHException
:
2342 except socket
.gaierror
:
2343 log
.info("Error With URL "+httpurl
);
2345 except socket
.timeout
:
2346 log
.info("Error With URL "+httpurl
);
2348 sftp
= ssh
.open_sftp();
2349 sftp
.putfo(sftpfile
, urlparts
.path
);
2352 sftpfile
.seek(0, 0);
2355 def upload_file_to_pysftp_file(sftpfile
, url
):
2359 def upload_file_to_pysftp_string(sftpstring
, url
):
2360 sftpfileo
= BytesIO(sftpstring
);
2361 sftpfile
= upload_file_to_pysftp_files(ftpfileo
, url
);
2365 def upload_file_to_pysftp_string(url
):