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
;
26 from urlparse
import parse_qsl
;
27 except DeprecationWarning:
28 from urlparse
import parse_qsl
;
35 havemechanize
= False;
40 havemechanize
= False;
67 from httplib2
import HTTPConnectionWithTimeout
, HTTPSConnectionWithTimeout
;
107 if(sys
.version
[0]=="2"):
109 from io
import StringIO
, BytesIO
;
112 from cStringIO
import StringIO
;
113 from cStringIO
import StringIO
as BytesIO
;
115 from StringIO
import StringIO
;
116 from StringIO
import StringIO
as BytesIO
;
117 # From http://python-future.org/compatible_idioms.html
118 from urlparse
import urlparse
, urlunparse
, urlsplit
, urlunsplit
, urljoin
;
119 from urllib
import urlencode
;
120 from urllib
import urlopen
as urlopenalt
;
121 from urllib2
import urlopen
, Request
, install_opener
, HTTPError
, URLError
, build_opener
, HTTPCookieProcessor
;
122 import urlparse
, cookielib
;
123 from httplib
import HTTPConnection
, HTTPSConnection
;
124 if(sys
.version
[0]>="3"):
125 from io
import StringIO
, BytesIO
;
126 # From http://python-future.org/compatible_idioms.html
127 from urllib
.parse
import urlparse
, urlunparse
, urlsplit
, urlunsplit
, urljoin
, urlencode
;
128 from urllib
.request
import urlopen
, Request
, install_opener
, build_opener
, HTTPCookieProcessor
;
129 from urllib
.error
import HTTPError
, URLError
;
130 import urllib
.parse
as urlparse
;
131 import http
.cookiejar
as cookielib
;
132 from http
.client
import HTTPConnection
, HTTPSConnection
;
134 __program_name__
= "PyWWW-Get";
135 __program_alt_name__
= "PyWWWGet";
136 __program_small_name__
= "wwwget";
137 __project__
= __program_name__
;
138 __project_url__
= "https://github.com/GameMaker2k/PyWWW-Get";
139 __version_info__
= (2, 0, 2, "RC 1", 1);
140 __version_date_info__
= (2023, 10, 5, "RC 1", 1);
141 __version_date__
= str(__version_date_info__
[0])+"."+str(__version_date_info__
[1]).zfill(2)+"."+str(__version_date_info__
[2]).zfill(2);
142 __revision__
= __version_info__
[3];
143 __revision_id__
= "$Id$";
144 if(__version_info__
[4] is not None):
145 __version_date_plusrc__
= __version_date__
+"-"+str(__version_date_info__
[4]);
146 if(__version_info__
[4] is None):
147 __version_date_plusrc__
= __version_date__
;
148 if(__version_info__
[3] is not None):
149 __version__
= str(__version_info__
[0])+"."+str(__version_info__
[1])+"."+str(__version_info__
[2])+" "+str(__version_info__
[3]);
150 if(__version_info__
[3] is None):
151 __version__
= str(__version_info__
[0])+"."+str(__version_info__
[1])+"."+str(__version_info__
[2]);
153 tmpfileprefix
= "py"+str(sys
.version_info
[0])+__program_small_name__
+str(__version_info__
[0])+"-";
155 pytempdir
= tempfile
.gettempdir();
157 PyBitness
= platform
.architecture();
158 if(PyBitness
=="32bit" or PyBitness
=="32"):
160 elif(PyBitness
=="64bit" or PyBitness
=="64"):
165 compression_supported_list
= ['identity', 'gzip', 'deflate', 'bzip2'];
167 compression_supported_list
.append('br');
169 compression_supported_list
.append('zstd');
171 compression_supported_list
.append('lzma');
172 compression_supported_list
.append('xz');
173 compression_supported
= ', '.join(compression_supported_list
);
175 geturls_cj
= cookielib
.CookieJar();
176 windowsNT4_ua_string
= "Windows NT 4.0";
177 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"};
178 windows2k_ua_string
= "Windows NT 5.0";
179 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"};
180 windowsXP_ua_string
= "Windows NT 5.1";
181 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"};
182 windowsXP64_ua_string
= "Windows NT 5.2; Win64; x64";
183 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"};
184 windows7_ua_string
= "Windows NT 6.1; Win64; x64";
185 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"};
186 windows8_ua_string
= "Windows NT 6.2; Win64; x64";
187 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"};
188 windows81_ua_string
= "Windows NT 6.3; Win64; x64";
189 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"};
190 windows10_ua_string
= "Windows NT 10.0; Win64; x64";
191 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"};
192 windows11_ua_string
= "Windows NT 11.0; Win64; x64";
193 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"};
194 geturls_ua_firefox_windows7
= "Mozilla/5.0 ("+windows7_ua_string
+"; rv:109.0) Gecko/20100101 Firefox/117.0";
195 geturls_ua_seamonkey_windows7
= "Mozilla/5.0 ("+windows7_ua_string
+"; rv:91.0) Gecko/20100101 Firefox/91.0 SeaMonkey/2.53.17";
196 geturls_ua_chrome_windows7
= "Mozilla/5.0 ("+windows7_ua_string
+") AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36";
197 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";
198 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";
199 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";
200 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";
201 geturls_ua_internet_explorer_windows7
= "Mozilla/5.0 ("+windows7_ua_string
+"; Trident/7.0; rv:11.0) like Gecko";
202 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";
203 geturls_ua_pywwwget_python
= "Mozilla/5.0 (compatible; {proname}/{prover}; +{prourl})".format(proname
=__project__
, prover
=__version__
, prourl
=__project_url__
);
204 if(platform
.python_implementation()!=""):
205 py_implementation
= platform
.python_implementation();
206 if(platform
.python_implementation()==""):
207 py_implementation
= "Python";
208 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__
);
209 geturls_ua_googlebot_google
= "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)";
210 geturls_ua_googlebot_google_old
= "Googlebot/2.1 (+http://www.google.com/bot.html)";
211 geturls_ua
= geturls_ua_firefox_windows7
;
212 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"};
213 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"};
214 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"};
215 geturls_headers_chrome_windows7
.update(windows7_ua_addon
);
216 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"};
217 geturls_headers_chromium_windows7
.update(windows7_ua_addon
);
218 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"};
219 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"};
220 geturls_headers_opera_windows7
.update(windows7_ua_addon
);
221 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"};
222 geturls_headers_vivaldi_windows7
.update(windows7_ua_addon
);
223 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"};
224 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"}
225 geturls_headers_microsoft_edge_windows7
.update(windows7_ua_addon
);
226 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
)};
227 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
)};
228 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"};
229 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"};
230 geturls_headers
= geturls_headers_firefox_windows7
;
231 geturls_download_sleep
= 0;
233 def verbose_printout(dbgtxt
, outtype
="log", dbgenable
=True, dgblevel
=20):
234 if(outtype
=="print" and dbgenable
):
237 elif(outtype
=="log" and dbgenable
):
238 logging
.info(dbgtxt
);
240 elif(outtype
=="warning" and dbgenable
):
241 logging
.warning(dbgtxt
);
243 elif(outtype
=="error" and dbgenable
):
244 logging
.error(dbgtxt
);
246 elif(outtype
=="critical" and dbgenable
):
247 logging
.critical(dbgtxt
);
249 elif(outtype
=="exception" and dbgenable
):
250 logging
.exception(dbgtxt
);
252 elif(outtype
=="logalt" and dbgenable
):
253 logging
.log(dgblevel
, dbgtxt
);
255 elif(outtype
=="debug" and dbgenable
):
256 logging
.debug(dbgtxt
);
264 def verbose_printout_return(dbgtxt
, outtype
="log", dbgenable
=True, dgblevel
=20):
265 dbgout
= verbose_printout(dbgtxt
, outtype
, dbgenable
, dgblevel
);
270 def add_url_param(url
, **params
):
272 parts
= list(urlparse
.urlsplit(url
));
273 d
= dict(parse_qsl(parts
[n
])); # use cgi.parse_qs for list values
275 parts
[n
]=urlencode(d
);
276 return urlparse
.urlunsplit(parts
);
278 os
.environ
["PATH"] = os
.environ
["PATH"] + os
.pathsep
+ os
.path
.dirname(os
.path
.realpath(__file__
)) + os
.pathsep
+ os
.getcwd();
279 def which_exec(execfile):
280 for path
in os
.environ
["PATH"].split(":"):
281 if os
.path
.exists(path
+ "/" + execfile):
282 return path
+ "/" + execfile;
284 def listize(varlist
):
292 newlistreg
.update({ilx
: varlist
[il
]});
293 newlistrev
.update({varlist
[il
]: ilx
});
296 newlistfull
= {1: newlistreg
, 2: newlistrev
, 'reg': newlistreg
, 'rev': newlistrev
};
299 def twolistize(varlist
):
309 newlistnamereg
.update({ilx
: varlist
[il
][0].strip()});
310 newlistnamerev
.update({varlist
[il
][0].strip(): ilx
});
311 newlistdescreg
.update({ilx
: varlist
[il
][1].strip()});
312 newlistdescrev
.update({varlist
[il
][1].strip(): ilx
});
315 newlistnametmp
= {1: newlistnamereg
, 2: newlistnamerev
, 'reg': newlistnamereg
, 'rev': newlistnamerev
};
316 newlistdesctmp
= {1: newlistdescreg
, 2: newlistdescrev
, 'reg': newlistdescreg
, 'rev': newlistdescrev
};
317 newlistfull
= {1: newlistnametmp
, 2: newlistdesctmp
, 'name': newlistnametmp
, 'desc': newlistdesctmp
}
320 def arglistize(proexec
, *varlist
):
324 newarglist
= [proexec
];
326 if varlist
[il
][0] is not None:
327 newarglist
.append(varlist
[il
][0]);
328 if varlist
[il
][1] is not None:
329 newarglist
.append(varlist
[il
][1]);
333 def fix_header_names(header_dict
):
334 if(sys
.version
[0]=="2"):
335 header_dict
= {k
.title(): v
for k
, v
in header_dict
.iteritems()};
336 if(sys
.version
[0]>="3"):
337 header_dict
= {k
.title(): v
for k
, v
in header_dict
.items()};
340 # hms_string by ArcGIS Python Recipes
341 # https://arcpy.wordpress.com/2012/04/20/146/
342 def hms_string(sec_elapsed
):
343 h
= int(sec_elapsed
/ (60 * 60));
344 m
= int((sec_elapsed
% (60 * 60)) / 60);
345 s
= sec_elapsed
% 60.0;
346 return "{}:{:>02}:{:>05.2f}".format(h
, m
, s
);
348 # get_readable_size by Lipis
349 # http://stackoverflow.com/posts/14998888/revisions
350 def get_readable_size(bytes
, precision
=1, unit
="IEC"):
352 if(unit
!="IEC" and unit
!="SI"):
355 units
= [" B"," KiB"," MiB"," GiB"," TiB"," PiB"," EiB"," ZiB"];
356 unitswos
= ["B","KiB","MiB","GiB","TiB","PiB","EiB","ZiB"];
359 units
= [" B"," kB"," MB"," GB"," TB"," PB"," EB"," ZB"];
360 unitswos
= ["B","kB","MB","GB","TB","PB","EB","ZB"];
365 if abs(bytes
) < unitsize
:
366 strformat
= "%3."+str(precision
)+"f%s";
367 pre_return_val
= (strformat
% (bytes
, unit
));
368 pre_return_val
= re
.sub(r
"([0]+) ([A-Za-z]+)", r
" \2", pre_return_val
);
369 pre_return_val
= re
.sub(r
"\. ([A-Za-z]+)", r
" \1", pre_return_val
);
370 alt_return_val
= pre_return_val
.split();
371 return_val
= {'Bytes': orgbytes
, 'ReadableWithSuffix': pre_return_val
, 'ReadableWithoutSuffix': alt_return_val
[0], 'ReadableSuffix': alt_return_val
[1]}
374 strformat
= "%."+str(precision
)+"f%s";
375 pre_return_val
= (strformat
% (bytes
, "YiB"));
376 pre_return_val
= re
.sub(r
"([0]+) ([A-Za-z]+)", r
" \2", pre_return_val
);
377 pre_return_val
= re
.sub(r
"\. ([A-Za-z]+)", r
" \1", pre_return_val
);
378 alt_return_val
= pre_return_val
.split();
379 return_val
= {'Bytes': orgbytes
, 'ReadableWithSuffix': pre_return_val
, 'ReadableWithoutSuffix': alt_return_val
[0], 'ReadableSuffix': alt_return_val
[1]}
382 def get_readable_size_from_file(infile
, precision
=1, unit
="IEC", usehashes
=False, usehashtypes
="md5,sha1"):
384 usehashtypes
= usehashtypes
.lower();
385 getfilesize
= os
.path
.getsize(infile
);
386 return_val
= get_readable_size(getfilesize
, precision
, unit
);
388 hashtypelist
= usehashtypes
.split(",");
389 openfile
= open(infile
, "rb");
390 filecontents
= openfile
.read();
393 listnumend
= len(hashtypelist
);
394 while(listnumcount
< listnumend
):
395 hashtypelistlow
= hashtypelist
[listnumcount
].strip();
396 hashtypelistup
= hashtypelistlow
.upper();
397 filehash
= hashlib
.new(hashtypelistup
);
398 filehash
.update(filecontents
);
399 filegethash
= filehash
.hexdigest();
400 return_val
.update({hashtypelistup
: filegethash
});
404 def get_readable_size_from_string(instring
, precision
=1, unit
="IEC", usehashes
=False, usehashtypes
="md5,sha1"):
406 usehashtypes
= usehashtypes
.lower();
407 getfilesize
= len(instring
);
408 return_val
= get_readable_size(getfilesize
, precision
, unit
);
410 hashtypelist
= usehashtypes
.split(",");
412 listnumend
= len(hashtypelist
);
413 while(listnumcount
< listnumend
):
414 hashtypelistlow
= hashtypelist
[listnumcount
].strip();
415 hashtypelistup
= hashtypelistlow
.upper();
416 filehash
= hashlib
.new(hashtypelistup
);
417 if(sys
.version
[0]=="2"):
418 filehash
.update(instring
);
419 if(sys
.version
[0]>="3"):
420 filehash
.update(instring
.encode('utf-8'));
421 filegethash
= filehash
.hexdigest();
422 return_val
.update({hashtypelistup
: filegethash
});
426 def http_status_to_reason(code
):
429 101: 'Switching Protocols',
434 203: 'Non-Authoritative Information',
436 205: 'Reset Content',
437 206: 'Partial Content',
439 208: 'Already Reported',
441 300: 'Multiple Choices',
442 301: 'Moved Permanently',
447 307: 'Temporary Redirect',
448 308: 'Permanent Redirect',
451 402: 'Payment Required',
454 405: 'Method Not Allowed',
455 406: 'Not Acceptable',
456 407: 'Proxy Authentication Required',
457 408: 'Request Timeout',
460 411: 'Length Required',
461 412: 'Precondition Failed',
462 413: 'Payload Too Large',
464 415: 'Unsupported Media Type',
465 416: 'Range Not Satisfiable',
466 417: 'Expectation Failed',
467 421: 'Misdirected Request',
468 422: 'Unprocessable Entity',
470 424: 'Failed Dependency',
471 426: 'Upgrade Required',
472 428: 'Precondition Required',
473 429: 'Too Many Requests',
474 431: 'Request Header Fields Too Large',
475 451: 'Unavailable For Legal Reasons',
476 500: 'Internal Server Error',
477 501: 'Not Implemented',
479 503: 'Service Unavailable',
480 504: 'Gateway Timeout',
481 505: 'HTTP Version Not Supported',
482 506: 'Variant Also Negotiates',
483 507: 'Insufficient Storage',
484 508: 'Loop Detected',
486 511: 'Network Authentication Required'
488 return reasons
.get(code
, 'Unknown Status Code');
490 def ftp_status_to_reason(code
):
492 110: 'Restart marker reply',
493 120: 'Service ready in nnn minutes',
494 125: 'Data connection already open; transfer starting',
495 150: 'File status okay; about to open data connection',
497 202: 'Command not implemented, superfluous at this site',
498 211: 'System status, or system help reply',
499 212: 'Directory status',
502 215: 'NAME system type',
503 220: 'Service ready for new user',
504 221: 'Service closing control connection',
505 225: 'Data connection open; no transfer in progress',
506 226: 'Closing data connection',
507 227: 'Entering Passive Mode',
508 230: 'User logged in, proceed',
509 250: 'Requested file action okay, completed',
510 257: '"PATHNAME" created',
511 331: 'User name okay, need password',
512 332: 'Need account for login',
513 350: 'Requested file action pending further information',
514 421: 'Service not available, closing control connection',
515 425: 'Can\'t open data connection',
516 426: 'Connection closed; transfer aborted',
517 450: 'Requested file action not taken',
518 451: 'Requested action aborted. Local error in processing',
519 452: 'Requested action not taken. Insufficient storage space in system',
520 500: 'Syntax error, command unrecognized',
521 501: 'Syntax error in parameters or arguments',
522 502: 'Command not implemented',
523 503: 'Bad sequence of commands',
524 504: 'Command not implemented for that parameter',
525 530: 'Not logged in',
526 532: 'Need account for storing files',
527 550: 'Requested action not taken. File unavailable',
528 551: 'Requested action aborted. Page type unknown',
529 552: 'Requested file action aborted. Exceeded storage allocation',
530 553: 'Requested action not taken. File name not allowed'
532 return reasons
.get(code
, 'Unknown Status Code');
534 def sftp_status_to_reason(code
):
538 2: 'SSH_FX_NO_SUCH_FILE',
539 3: 'SSH_FX_PERMISSION_DENIED',
541 5: 'SSH_FX_BAD_MESSAGE',
542 6: 'SSH_FX_NO_CONNECTION',
543 7: 'SSH_FX_CONNECTION_LOST',
544 8: 'SSH_FX_OP_UNSUPPORTED'
546 return reasons
.get(code
, 'Unknown Status Code');
548 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"}):
549 if isinstance(headers
, dict):
551 if(sys
.version
[0]=="2"):
552 for headkey
, headvalue
in headers
.iteritems():
553 returnval
.append((headkey
, headvalue
));
554 if(sys
.version
[0]>="3"):
555 for headkey
, headvalue
in headers
.items():
556 returnval
.append((headkey
, headvalue
));
557 elif isinstance(headers
, list):
563 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"}):
564 if isinstance(headers
, dict):
566 if(sys
.version
[0]=="2"):
567 for headkey
, headvalue
in headers
.iteritems():
568 returnval
.append(headkey
+": "+headvalue
);
569 if(sys
.version
[0]>="3"):
570 for headkey
, headvalue
in headers
.items():
571 returnval
.append(headkey
+": "+headvalue
);
572 elif isinstance(headers
, list):
578 def make_http_headers_from_pycurl_to_dict(headers
):
580 headers
= headers
.strip().split('\r\n');
581 for header
in headers
:
582 parts
= header
.split(': ', 1)
585 header_dict
[key
.title()] = value
;
588 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")]):
589 if isinstance(headers
, list):
594 returnval
.update({headers
[mli
][0]: headers
[mli
][1]});
596 elif isinstance(headers
, dict):
602 def get_httplib_support(checkvalue
=None):
603 global haverequests
, havemechanize
, havehttplib2
, haveurllib3
, havehttpx
, havehttpcore
, haveparamiko
, havepysftp
;
605 returnval
.append("ftp");
606 returnval
.append("httplib");
608 returnval
.append("httplib2");
609 returnval
.append("urllib");
611 returnval
.append("urllib3");
612 returnval
.append("request3");
613 returnval
.append("request");
615 returnval
.append("requests");
617 returnval
.append("aiohttp");
619 returnval
.append("httpx");
620 returnval
.append("httpx2");
622 returnval
.append("mechanize");
624 returnval
.append("pycurl");
625 if(hasattr(pycurl
, "CURL_HTTP_VERSION_2_0")):
626 returnval
.append("pycurl2");
627 if(hasattr(pycurl
, "CURL_HTTP_VERSION_3_0")):
628 returnval
.append("pycurl3");
630 returnval
.append("sftp");
632 returnval
.append("pysftp");
633 if(not checkvalue
is None):
634 if(checkvalue
=="urllib1" or checkvalue
=="urllib2"):
635 checkvalue
= "urllib";
636 if(checkvalue
=="httplib1"):
637 checkvalue
= "httplib";
638 if(checkvalue
in returnval
):
644 def check_httplib_support(checkvalue
="urllib"):
645 if(checkvalue
=="urllib1" or checkvalue
=="urllib2"):
646 checkvalue
= "urllib";
647 if(checkvalue
=="httplib1"):
648 checkvalue
= "httplib";
649 returnval
= get_httplib_support(checkvalue
);
652 def get_httplib_support_list():
653 returnval
= get_httplib_support(None);
656 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):
657 global geturls_download_sleep
, havezstd
, havebrotli
, haveaiohttp
, haverequests
, havemechanize
, havepycurl
, havehttplib2
, haveurllib3
, havehttpx
, havehttpcore
, haveparamiko
, havepysftp
;
659 sleep
= geturls_download_sleep
;
662 if(httplibuse
=="urllib1" or httplibuse
=="urllib2" or httplibuse
=="request"):
663 httplibuse
= "urllib";
664 if(httplibuse
=="httplib1"):
665 httplibuse
= "httplib";
666 if(not haverequests
and httplibuse
=="requests"):
667 httplibuse
= "urllib";
668 if(not haveaiohttp
and httplibuse
=="aiohttp"):
669 httplibuse
= "urllib";
670 if(not havehttpx
and httplibuse
=="httpx"):
671 httplibuse
= "urllib";
672 if(not havehttpx
and httplibuse
=="httpx2"):
673 httplibuse
= "urllib";
674 if(not havehttpcore
and httplibuse
=="httpcore"):
675 httplibuse
= "urllib";
676 if(not havehttpcore
and httplibuse
=="httpcore2"):
677 httplibuse
= "urllib";
678 if(not havemechanize
and httplibuse
=="mechanize"):
679 httplibuse
= "urllib";
680 if(not havepycurl
and httplibuse
=="pycurl"):
681 httplibuse
= "urllib";
682 if(not havepycurl
and httplibuse
=="pycurl2"):
683 httplibuse
= "urllib";
684 if(havepycurl
and httplibuse
=="pycurl2" and not hasattr(pycurl
, "CURL_HTTP_VERSION_2_0")):
685 httplibuse
= "pycurl";
686 if(not havepycurl
and httplibuse
=="pycurl3"):
687 httplibuse
= "urllib";
688 if(havepycurl
and httplibuse
=="pycurl3" and not hasattr(pycurl
, "CURL_HTTP_VERSION_3_0") and hasattr(pycurl
, "CURL_HTTP_VERSION_2_0")):
689 httplibuse
= "pycurl2";
690 if(havepycurl
and httplibuse
=="pycurl3" and not hasattr(pycurl
, "CURL_HTTP_VERSION_3_0") and not hasattr(pycurl
, "CURL_HTTP_VERSION_2_0")):
691 httplibuse
= "pycurl";
692 if(not havehttplib2
and httplibuse
=="httplib2"):
693 httplibuse
= "httplib";
694 if(not haveparamiko
and httplibuse
=="sftp"):
696 if(not havepysftp
and httplibuse
=="pysftp"):
698 urlparts
= urlparse
.urlparse(httpurl
);
699 if(isinstance(httpheaders
, list)):
700 httpheaders
= make_http_headers_from_list_to_dict(httpheaders
);
701 httpheaders
= fix_header_names(httpheaders
);
702 if(httpuseragent
is not None):
703 if('User-Agent' in httpheaders
):
704 httpheaders
['User-Agent'] = httpuseragent
;
706 httpuseragent
.update({'User-Agent': httpuseragent
});
707 if(httpreferer
is not None):
708 if('Referer' in httpheaders
):
709 httpheaders
['Referer'] = httpreferer
;
711 httpuseragent
.update({'Referer': httpreferer
});
712 if(urlparts
.username
is not None or urlparts
.password
is not None):
713 if(sys
.version
[0]=="2"):
714 inurlencode
= b64encode(str(urlparts
.username
+":"+urlparts
.password
));
715 if(sys
.version
[0]>="3"):
716 inurlencode
= b64encode(str(urlparts
.username
+":"+urlparts
.password
).encode()).decode("UTF-8");
717 httpheaders
.update( { 'Authorization': "Basic "+inurlencode
} );
718 geturls_opener
= build_opener(HTTPCookieProcessor(httpcookie
));
719 if(httplibuse
=="urllib" or httplibuse
=="mechanize"):
720 if(isinstance(httpheaders
, dict)):
721 httpheaders
= make_http_headers_from_dict_to_list(httpheaders
);
722 if(httplibuse
=="pycurl" or httplibuse
=="pycurl2" or httplibuse
=="pycurl3"):
723 if(isinstance(httpheaders
, dict)):
724 httpheaders
= make_http_headers_from_dict_to_pycurl(httpheaders
);
725 geturls_opener
.addheaders
= httpheaders
;
727 if(postdata
is not None and not isinstance(postdata
, dict)):
728 postdata
= urlencode(postdata
);
729 if(httplibuse
=="urllib" or httplibuse
=="request"):
730 geturls_request
= Request(httpurl
);
732 if(httpmethod
=="GET"):
733 geturls_text
= geturls_opener
.open(geturls_request
);
734 elif(httpmethod
=="POST"):
735 geturls_text
= geturls_opener
.open(geturls_request
, data
=postdata
);
737 geturls_text
= geturls_opener
.open(geturls_request
);
738 except HTTPError
as geturls_text_error
:
739 geturls_text
= geturls_text_error
;
740 log
.info("Error With URL "+httpurl
);
742 log
.info("Error With URL "+httpurl
);
744 except socket
.timeout
:
745 log
.info("Error With URL "+httpurl
);
747 httpcodeout
= geturls_text
.getcode();
749 httpcodereason
= geturls_text
.reason
;
750 except AttributeError:
751 httpcodereason
= http_status_to_reason(geturls_text
.getcode());
753 httpversionout
= geturls_text
.version
;
754 except AttributeError:
755 httpversionout
= "1.1";
756 httpmethodout
= geturls_request
.get_method();
757 httpurlout
= geturls_text
.geturl();
758 httpheaderout
= geturls_text
.info();
759 httpheadersentout
= httpheaders
;
760 elif(httplibuse
=="httplib"):
761 if(urlparts
[0]=="http"):
762 httpconn
= HTTPConnection(urlparts
[1], timeout
=timeout
);
763 elif(urlparts
[0]=="https"):
764 httpconn
= HTTPSConnection(urlparts
[1], timeout
=timeout
);
767 if(postdata
is not None and not isinstance(postdata
, dict)):
768 postdata
= urlencode(postdata
);
770 if(httpmethod
=="GET"):
771 httpconn
.request("GET", urlparts
[2], headers
=httpheaders
);
772 elif(httpmethod
=="POST"):
773 httpconn
.request("GET", urlparts
[2], body
=postdata
, headers
=httpheaders
);
775 httpconn
.request("GET", urlparts
[2], headers
=httpheaders
);
776 except socket
.timeout
:
777 log
.info("Error With URL "+httpurl
);
779 except socket
.gaierror
:
780 log
.info("Error With URL "+httpurl
);
782 except BlockingIOError
:
783 log
.info("Error With URL "+httpurl
);
785 geturls_text
= httpconn
.getresponse();
786 httpcodeout
= geturls_text
.status
;
787 httpcodereason
= geturls_text
.reason
;
788 if(geturls_text
.version
=="10"):
789 httpversionout
= "1.0";
791 httpversionout
= "1.1";
792 httpmethodout
= geturls_text
._method
;
793 httpurlout
= httpurl
;
794 httpheaderout
= geturls_text
.getheaders();
795 httpheadersentout
= httpheaders
;
796 elif(httplibuse
=="httplib2"):
797 if(urlparts
[0]=="http"):
798 httpconn
= HTTPConnectionWithTimeout(urlparts
[1], timeout
=timeout
);
799 elif(urlparts
[0]=="https"):
800 httpconn
= HTTPSConnectionWithTimeout(urlparts
[1], timeout
=timeout
);
803 if(postdata
is not None and not isinstance(postdata
, dict)):
804 postdata
= urlencode(postdata
);
806 if(httpmethod
=="GET"):
807 httpconn
.request("GET", urlparts
[2], headers
=httpheaders
);
808 elif(httpmethod
=="POST"):
809 httpconn
.request("GET", urlparts
[2], body
=postdata
, headers
=httpheaders
);
811 httpconn
.request("GET", urlparts
[2], headers
=httpheaders
);
812 except socket
.timeout
:
813 log
.info("Error With URL "+httpurl
);
815 except socket
.gaierror
:
816 log
.info("Error With URL "+httpurl
);
818 except BlockingIOError
:
819 log
.info("Error With URL "+httpurl
);
821 geturls_text
= httpconn
.getresponse();
822 httpcodeout
= geturls_text
.status
;
823 httpcodereason
= geturls_text
.reason
;
824 if(geturls_text
.version
=="10"):
825 httpversionout
= "1.0";
827 httpversionout
= "1.1";
828 httpmethodout
= httpmethod
;
829 httpurlout
= httpurl
;
830 httpheaderout
= geturls_text
.getheaders();
831 httpheadersentout
= httpheaders
;
832 elif(httplibuse
=="urllib3" or httplibuse
=="request3"):
833 timeout
= urllib3
.util
.Timeout(connect
=timeout
, read
=timeout
);
834 urllib_pool
= urllib3
.PoolManager(headers
=httpheaders
, timeout
=timeout
);
836 if(httpmethod
=="GET"):
837 geturls_text
= urllib_pool
.request("GET", httpurl
, headers
=httpheaders
, preload_content
=False);
838 elif(httpmethod
=="POST"):
839 geturls_text
= urllib_pool
.request("POST", httpurl
, body
=postdata
, headers
=httpheaders
, preload_content
=False);
841 geturls_text
= urllib_pool
.request("GET", httpurl
, headers
=httpheaders
, preload_content
=False);
842 except urllib3
.exceptions
.ConnectTimeoutError
:
843 log
.info("Error With URL "+httpurl
);
845 except urllib3
.exceptions
.ConnectError
:
846 log
.info("Error With URL "+httpurl
);
848 except urllib3
.exceptions
.MaxRetryError
:
849 log
.info("Error With URL "+httpurl
);
851 except socket
.timeout
:
852 log
.info("Error With URL "+httpurl
);
855 log
.info("Error With URL "+httpurl
);
857 httpcodeout
= geturls_text
.status
;
858 httpcodereason
= geturls_text
.reason
;
859 if(geturls_text
.version
=="10"):
860 httpversionout
= "1.0";
862 httpversionout
= "1.1";
863 httpmethodout
= httpmethod
;
864 httpurlout
= geturls_text
.geturl();
865 httpheaderout
= geturls_text
.info();
866 httpheadersentout
= httpheaders
;
867 elif(httplibuse
=="requests"):
869 reqsession
= requests
.Session();
870 if(httpmethod
=="GET"):
871 geturls_text
= reqsession
.get(httpurl
, timeout
=timeout
, headers
=httpheaders
, cookies
=httpcookie
);
872 elif(httpmethod
=="POST"):
873 geturls_text
= reqsession
.post(httpurl
, timeout
=timeout
, data
=postdata
, headers
=httpheaders
, cookies
=httpcookie
);
875 geturls_text
= reqsession
.get(httpurl
, timeout
=timeout
, headers
=httpheaders
, cookies
=httpcookie
);
876 except requests
.exceptions
.ConnectTimeout
:
877 log
.info("Error With URL "+httpurl
);
879 except requests
.exceptions
.ConnectError
:
880 log
.info("Error With URL "+httpurl
);
882 except socket
.timeout
:
883 log
.info("Error With URL "+httpurl
);
885 httpcodeout
= geturls_text
.status_code
;
886 httpcodereason
= geturls_text
.reason
;
887 if(geturls_text
.raw
.version
=="10"):
888 httpversionout
= "1.0";
890 httpversionout
= "1.1";
891 httpmethodout
= httpmethod
;
892 httpurlout
= geturls_text
.url
;
893 httpheaderout
= geturls_text
.headers
;
894 httpheadersentout
= geturls_text
.request
.headers
;
895 elif(httplibuse
=="aiohttp"):
897 reqsession
= aiohttp
.ClientSession(cookie_jar
=httpcookie
, headers
=httpheaders
, timeout
=timeout
, read_timeout
=timeout
, conn_timeout
=timeout
, read_bufsize
=buffersize
);
898 if(httpmethod
=="GET"):
899 geturls_text
= reqsession
.get(httpurl
);
900 elif(httpmethod
=="POST"):
901 geturls_text
= reqsession
.post(httpurl
, data
=postdata
);
903 geturls_text
= reqsession
.get(httpurl
);
904 except aiohttp
.exceptions
.ConnectTimeout
:
905 log
.info("Error With URL "+httpurl
);
907 except aiohttp
.exceptions
.ConnectError
:
908 log
.info("Error With URL "+httpurl
);
910 except socket
.timeout
:
911 log
.info("Error With URL "+httpurl
);
913 httpcodeout
= geturls_text
.status
;
914 httpcodereason
= geturls_text
.reason
;
915 httpversionout
= geturls_text
.version
;
916 httpmethodout
= geturls_text
.method
;
917 httpurlout
= geturls_text
.url
;
918 httpheaderout
= geturls_text
.headers
;
919 httpheadersentout
= geturls_text
.request_info
.headers
;
920 elif(httplibuse
=="httpx"):
922 if(httpmethod
=="GET"):
923 httpx_pool
= httpx
.Client(http1
=True, http2
=False, trust_env
=True);
924 geturls_text
= httpx_pool
.get(httpurl
, timeout
=timeout
, headers
=httpheaders
, cookies
=httpcookie
);
925 elif(httpmethod
=="POST"):
926 httpx_pool
= httpx
.Client(http1
=True, http2
=False, trust_env
=True);
927 geturls_text
= httpx_pool
.post(httpurl
, timeout
=timeout
, data
=postdata
, headers
=httpheaders
, cookies
=httpcookie
);
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 except httpx
.ConnectTimeout
:
932 log
.info("Error With URL "+httpurl
);
934 except httpx
.ConnectError
:
935 log
.info("Error With URL "+httpurl
);
937 except socket
.timeout
:
938 log
.info("Error With URL "+httpurl
);
940 httpcodeout
= geturls_text
.status_code
;
942 httpcodereason
= geturls_text
.reason_phrase
;
944 httpcodereason
= http_status_to_reason(geturls_text
.status_code
);
945 httpversionout
= geturls_text
.http_version
;
946 httpmethodout
= httpmethod
;
947 httpurlout
= str(geturls_text
.url
);
948 httpheaderout
= geturls_text
.headers
;
949 httpheadersentout
= geturls_text
.request
.headers
;
950 elif(httplibuse
=="httpx2"):
952 if(httpmethod
=="GET"):
953 httpx_pool
= httpx
.Client(http1
=True, http2
=True, trust_env
=True);
954 geturls_text
= httpx_pool
.get(httpurl
, timeout
=timeout
, headers
=httpheaders
, cookies
=httpcookie
);
955 elif(httpmethod
=="POST"):
956 httpx_pool
= httpx
.Client(http1
=True, http2
=True, trust_env
=True);
957 geturls_text
= httpx_pool
.post(httpurl
, timeout
=timeout
, data
=postdata
, headers
=httpheaders
, cookies
=httpcookie
);
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 except httpx
.ConnectTimeout
:
962 log
.info("Error With URL "+httpurl
);
964 except httpx
.ConnectError
:
965 log
.info("Error With URL "+httpurl
);
967 except socket
.timeout
:
968 log
.info("Error With URL "+httpurl
);
970 httpcodeout
= geturls_text
.status_code
;
972 httpcodereason
= geturls_text
.reason_phrase
;
974 httpcodereason
= http_status_to_reason(geturls_text
.status_code
);
975 httpversionout
= geturls_text
.http_version
;
976 httpmethodout
= httpmethod
;
977 httpurlout
= str(geturls_text
.url
);
978 httpheaderout
= geturls_text
.headers
;
979 httpheadersentout
= geturls_text
.request
.headers
;
980 elif(httplibuse
=="httpcore"):
982 if(httpmethod
=="GET"):
983 httpx_pool
= httpcore
.ConnectionPool(http1
=True, http2
=False);
984 geturls_text
= httpx_pool
.request("GET", httpurl
, headers
=httpheaders
);
985 elif(httpmethod
=="POST"):
986 httpx_pool
= httpcore
.ConnectionPool(http1
=True, http2
=False);
987 geturls_text
= httpx_pool
.request("GET", httpurl
, data
=postdata
, headers
=httpheaders
);
989 httpx_pool
= httpcore
.ConnectionPool(http1
=True, http2
=False);
990 geturls_text
= httpx_pool
.request("GET", httpurl
, headers
=httpheaders
);
991 except httpcore
.ConnectTimeout
:
992 log
.info("Error With URL "+httpurl
);
994 except httpcore
.ConnectError
:
995 log
.info("Error With URL "+httpurl
);
997 except socket
.timeout
:
998 log
.info("Error With URL "+httpurl
);
1000 httpcodeout
= geturls_text
.status
;
1001 httpcodereason
= http_status_to_reason(geturls_text
.status
);
1002 httpversionout
= "1.1";
1003 httpmethodout
= httpmethod
;
1004 httpurlout
= str(httpurl
);
1005 httpheaderout
= geturls_text
.headers
;
1006 httpheadersentout
= httpheaders
;
1007 elif(httplibuse
=="httpcore2"):
1009 if(httpmethod
=="GET"):
1010 httpx_pool
= httpcore
.ConnectionPool(http1
=True, http2
=True);
1011 geturls_text
= httpx_pool
.request("GET", httpurl
, headers
=httpheaders
);
1012 elif(httpmethod
=="POST"):
1013 httpx_pool
= httpcore
.ConnectionPool(http1
=True, http2
=True);
1014 geturls_text
= httpx_pool
.request("GET", httpurl
, data
=postdata
, headers
=httpheaders
);
1016 httpx_pool
= httpcore
.ConnectionPool(http1
=True, http2
=True);
1017 geturls_text
= httpx_pool
.request("GET", httpurl
, headers
=httpheaders
);
1018 except httpcore
.ConnectTimeout
:
1019 log
.info("Error With URL "+httpurl
);
1021 except httpcore
.ConnectError
:
1022 log
.info("Error With URL "+httpurl
);
1024 except socket
.timeout
:
1025 log
.info("Error With URL "+httpurl
);
1027 httpcodeout
= geturls_text
.status
;
1028 httpcodereason
= http_status_to_reason(geturls_text
.status
);
1029 httpversionout
= "1.1";
1030 httpmethodout
= httpmethod
;
1031 httpurlout
= str(httpurl
);
1032 httpheaderout
= geturls_text
.headers
;
1033 httpheadersentout
= httpheaders
;
1034 elif(httplibuse
=="mechanize"):
1035 geturls_opener
= mechanize
.Browser();
1036 if(isinstance(httpheaders
, dict)):
1037 httpheaders
= make_http_headers_from_dict_to_list(httpheaders
);
1039 geturls_opener
.addheaders
= httpheaders
;
1040 geturls_opener
.set_cookiejar(httpcookie
);
1041 geturls_opener
.set_handle_robots(False);
1042 if(postdata
is not None and not isinstance(postdata
, dict)):
1043 postdata
= urlencode(postdata
);
1045 if(httpmethod
=="GET"):
1046 geturls_text
= geturls_opener
.open(httpurl
);
1047 elif(httpmethod
=="POST"):
1048 geturls_text
= geturls_opener
.open(httpurl
, data
=postdata
);
1050 geturls_text
= geturls_opener
.open(httpurl
);
1051 except mechanize
.HTTPError
as geturls_text_error
:
1052 geturls_text
= geturls_text_error
;
1053 log
.info("Error With URL "+httpurl
);
1055 log
.info("Error With URL "+httpurl
);
1057 except socket
.timeout
:
1058 log
.info("Error With URL "+httpurl
);
1060 httpcodeout
= geturls_text
.code
;
1061 httpcodereason
= geturls_text
.msg
;
1062 httpversionout
= "1.1";
1063 httpmethodout
= httpmethod
;
1064 httpurlout
= geturls_text
.geturl();
1065 httpheaderout
= geturls_text
.info();
1066 reqhead
= geturls_opener
.request
;
1067 httpheadersentout
= reqhead
.header_items();
1068 elif(httplibuse
=="pycurl"):
1069 retrieved_body
= BytesIO();
1070 retrieved_headers
= BytesIO();
1072 if(httpmethod
=="GET"):
1073 geturls_text
= pycurl
.Curl();
1074 geturls_text
.setopt(geturls_text
.URL
, httpurl
);
1075 geturls_text
.setopt(geturls_text
.HTTP_VERSION
, geturls_text
.CURL_HTTP_VERSION_1_1
);
1076 geturls_text
.setopt(geturls_text
.WRITEFUNCTION
, retrieved_body
.write
);
1077 geturls_text
.setopt(geturls_text
.HTTPHEADER
, httpheaders
);
1078 geturls_text
.setopt(geturls_text
.HEADERFUNCTION
, retrieved_headers
.write
);
1079 geturls_text
.setopt(geturls_text
.FOLLOWLOCATION
, True);
1080 geturls_text
.setopt(geturls_text
.TIMEOUT
, timeout
);
1081 geturls_text
.perform();
1082 elif(httpmethod
=="POST"):
1083 geturls_text
= pycurl
.Curl();
1084 geturls_text
.setopt(geturls_text
.URL
, httpurl
);
1085 geturls_text
.setopt(geturls_text
.HTTP_VERSION
, geturls_text
.CURL_HTTP_VERSION_1_1
);
1086 geturls_text
.setopt(geturls_text
.WRITEFUNCTION
, retrieved_body
.write
);
1087 geturls_text
.setopt(geturls_text
.HTTPHEADER
, httpheaders
);
1088 geturls_text
.setopt(geturls_text
.HEADERFUNCTION
, retrieved_headers
.write
);
1089 geturls_text
.setopt(geturls_text
.FOLLOWLOCATION
, True);
1090 geturls_text
.setopt(geturls_text
.TIMEOUT
, timeout
);
1091 geturls_text
.setopt(geturls_text
.POST
, True);
1092 geturls_text
.setopt(geturls_text
.POSTFIELDS
, postdata
);
1093 geturls_text
.perform();
1095 geturls_text
= pycurl
.Curl();
1096 geturls_text
.setopt(geturls_text
.URL
, httpurl
);
1097 geturls_text
.setopt(geturls_text
.HTTP_VERSION
, geturls_text
.CURL_HTTP_VERSION_1_1
);
1098 geturls_text
.setopt(geturls_text
.WRITEFUNCTION
, retrieved_body
.write
);
1099 geturls_text
.setopt(geturls_text
.HTTPHEADER
, httpheaders
);
1100 geturls_text
.setopt(geturls_text
.HEADERFUNCTION
, retrieved_headers
.write
);
1101 geturls_text
.setopt(geturls_text
.FOLLOWLOCATION
, True);
1102 geturls_text
.setopt(geturls_text
.TIMEOUT
, timeout
);
1103 geturls_text
.perform();
1104 retrieved_headers
.seek(0);
1105 if(sys
.version
[0]=="2"):
1106 pycurlhead
= retrieved_headers
.read();
1107 if(sys
.version
[0]>="3"):
1108 pycurlhead
= retrieved_headers
.read().decode('UTF-8');
1109 pyhttpverinfo
= re
.findall(r
'^HTTP/([0-9.]+) (\d+)(?: ([A-Za-z\s]+))?$', pycurlhead
.splitlines()[0].strip().rstrip('\r\n'))[0];
1110 pycurlheadersout
= make_http_headers_from_pycurl_to_dict(pycurlhead
);
1111 retrieved_body
.seek(0);
1112 except socket
.timeout
:
1113 log
.info("Error With URL "+httpurl
);
1115 except socket
.gaierror
:
1116 log
.info("Error With URL "+httpurl
);
1119 log
.info("Error With URL "+httpurl
);
1121 httpcodeout
= geturls_text
.getinfo(geturls_text
.HTTP_CODE
);
1122 httpcodereason
= http_status_to_reason(geturls_text
.getinfo(geturls_text
.HTTP_CODE
));
1123 httpversionout
= pyhttpverinfo
[0];
1124 httpmethodout
= httpmethod
;
1125 httpurlout
= geturls_text
.getinfo(geturls_text
.EFFECTIVE_URL
);
1126 httpheaderout
= pycurlheadersout
;
1127 httpheadersentout
= httpheaders
;
1128 elif(httplibuse
=="pycurl2"):
1129 retrieved_body
= BytesIO();
1130 retrieved_headers
= BytesIO();
1132 if(httpmethod
=="GET"):
1133 geturls_text
= pycurl
.Curl();
1134 geturls_text
.setopt(geturls_text
.HTTP_VERSION
, geturls_text
.CURL_HTTP_VERSION_2_0
);
1135 geturls_text
.setopt(geturls_text
.URL
, httpurl
);
1136 geturls_text
.setopt(geturls_text
.WRITEFUNCTION
, retrieved_body
.write
);
1137 geturls_text
.setopt(geturls_text
.HTTPHEADER
, httpheaders
);
1138 geturls_text
.setopt(geturls_text
.HEADERFUNCTION
, retrieved_headers
.write
);
1139 geturls_text
.setopt(geturls_text
.FOLLOWLOCATION
, True);
1140 geturls_text
.setopt(geturls_text
.TIMEOUT
, timeout
);
1141 geturls_text
.perform();
1142 elif(httpmethod
=="POST"):
1143 geturls_text
= pycurl
.Curl();
1144 geturls_text
.setopt(geturls_text
.HTTP_VERSION
, geturls_text
.CURL_HTTP_VERSION_2_0
);
1145 geturls_text
.setopt(geturls_text
.URL
, httpurl
);
1146 geturls_text
.setopt(geturls_text
.WRITEFUNCTION
, retrieved_body
.write
);
1147 geturls_text
.setopt(geturls_text
.HTTPHEADER
, httpheaders
);
1148 geturls_text
.setopt(geturls_text
.HEADERFUNCTION
, retrieved_headers
.write
);
1149 geturls_text
.setopt(geturls_text
.FOLLOWLOCATION
, True);
1150 geturls_text
.setopt(geturls_text
.TIMEOUT
, timeout
);
1151 geturls_text
.setopt(geturls_text
.POST
, True);
1152 geturls_text
.setopt(geturls_text
.POSTFIELDS
, postdata
);
1153 geturls_text
.perform();
1155 geturls_text
= pycurl
.Curl();
1156 geturls_text
.setopt(geturls_text
.HTTP_VERSION
, geturls_text
.CURL_HTTP_VERSION_2_0
);
1157 geturls_text
.setopt(geturls_text
.URL
, httpurl
);
1158 geturls_text
.setopt(geturls_text
.WRITEFUNCTION
, retrieved_body
.write
);
1159 geturls_text
.setopt(geturls_text
.HTTPHEADER
, httpheaders
);
1160 geturls_text
.setopt(geturls_text
.HEADERFUNCTION
, retrieved_headers
.write
);
1161 geturls_text
.setopt(geturls_text
.FOLLOWLOCATION
, True);
1162 geturls_text
.setopt(geturls_text
.TIMEOUT
, timeout
);
1163 geturls_text
.perform();
1164 retrieved_headers
.seek(0);
1165 if(sys
.version
[0]=="2"):
1166 pycurlhead
= retrieved_headers
.read();
1167 if(sys
.version
[0]>="3"):
1168 pycurlhead
= retrieved_headers
.read().decode('UTF-8');
1169 pyhttpverinfo
= re
.findall(r
'^HTTP/([0-9.]+) (\d+)(?: ([A-Za-z\s]+))?$', pycurlhead
.splitlines()[0].strip())[0];
1170 pycurlheadersout
= make_http_headers_from_pycurl_to_dict(pycurlhead
);
1171 retrieved_body
.seek(0);
1172 except socket
.timeout
:
1173 log
.info("Error With URL "+httpurl
);
1175 except socket
.gaierror
:
1176 log
.info("Error With URL "+httpurl
);
1179 log
.info("Error With URL "+httpurl
);
1181 httpcodeout
= geturls_text
.getinfo(geturls_text
.HTTP_CODE
);
1182 httpcodereason
= http_status_to_reason(geturls_text
.getinfo(geturls_text
.HTTP_CODE
));
1183 httpversionout
= pyhttpverinfo
[0];
1184 httpmethodout
= httpmethod
;
1185 httpurlout
= geturls_text
.getinfo(geturls_text
.EFFECTIVE_URL
);
1186 httpheaderout
= pycurlheadersout
;
1187 httpheadersentout
= httpheaders
;
1188 elif(httplibuse
=="pycurl3"):
1189 retrieved_body
= BytesIO();
1190 retrieved_headers
= BytesIO();
1192 if(httpmethod
=="GET"):
1193 geturls_text
= pycurl
.Curl();
1194 geturls_text
.setopt(geturls_text
.HTTP_VERSION
, geturls_text
.CURL_HTTP_VERSION_3_0
);
1195 geturls_text
.setopt(geturls_text
.URL
, httpurl
);
1196 geturls_text
.setopt(geturls_text
.WRITEFUNCTION
, retrieved_body
.write
);
1197 geturls_text
.setopt(geturls_text
.HTTPHEADER
, httpheaders
);
1198 geturls_text
.setopt(geturls_text
.HEADERFUNCTION
, retrieved_headers
.write
);
1199 geturls_text
.setopt(geturls_text
.FOLLOWLOCATION
, True);
1200 geturls_text
.setopt(geturls_text
.TIMEOUT
, timeout
);
1201 geturls_text
.perform();
1202 elif(httpmethod
=="POST"):
1203 geturls_text
= pycurl
.Curl();
1204 geturls_text
.setopt(geturls_text
.HTTP_VERSION
, geturls_text
.CURL_HTTP_VERSION_3_0
);
1205 geturls_text
.setopt(geturls_text
.URL
, httpurl
);
1206 geturls_text
.setopt(geturls_text
.WRITEFUNCTION
, retrieved_body
.write
);
1207 geturls_text
.setopt(geturls_text
.HTTPHEADER
, httpheaders
);
1208 geturls_text
.setopt(geturls_text
.HEADERFUNCTION
, retrieved_headers
.write
);
1209 geturls_text
.setopt(geturls_text
.FOLLOWLOCATION
, True);
1210 geturls_text
.setopt(geturls_text
.TIMEOUT
, timeout
);
1211 geturls_text
.setopt(geturls_text
.POST
, True);
1212 geturls_text
.setopt(geturls_text
.POSTFIELDS
, postdata
);
1213 geturls_text
.perform();
1215 geturls_text
= pycurl
.Curl();
1216 geturls_text
.setopt(geturls_text
.HTTP_VERSION
, geturls_text
.CURL_HTTP_VERSION_3_0
);
1217 geturls_text
.setopt(geturls_text
.URL
, httpurl
);
1218 geturls_text
.setopt(geturls_text
.WRITEFUNCTION
, retrieved_body
.write
);
1219 geturls_text
.setopt(geturls_text
.HTTPHEADER
, httpheaders
);
1220 geturls_text
.setopt(geturls_text
.HEADERFUNCTION
, retrieved_headers
.write
);
1221 geturls_text
.setopt(geturls_text
.FOLLOWLOCATION
, True);
1222 geturls_text
.setopt(geturls_text
.TIMEOUT
, timeout
);
1223 geturls_text
.perform();
1224 retrieved_headers
.seek(0);
1225 if(sys
.version
[0]=="2"):
1226 pycurlhead
= retrieved_headers
.read();
1227 if(sys
.version
[0]>="3"):
1228 pycurlhead
= retrieved_headers
.read().decode('UTF-8');
1229 pyhttpverinfo
= re
.findall(r
'^HTTP/([0-9.]+) (\d+)(?: ([A-Za-z\s]+))?$', pycurlhead
.splitlines()[0].strip().rstrip('\r\n'))[0];
1230 pycurlheadersout
= make_http_headers_from_pycurl_to_dict(pycurlhead
);
1231 retrieved_body
.seek(0);
1232 except socket
.timeout
:
1233 log
.info("Error With URL "+httpurl
);
1235 except socket
.gaierror
:
1236 log
.info("Error With URL "+httpurl
);
1239 log
.info("Error With URL "+httpurl
);
1241 httpcodeout
= geturls_text
.getinfo(geturls_text
.HTTP_CODE
);
1242 httpcodereason
= http_status_to_reason(geturls_text
.getinfo(geturls_text
.HTTP_CODE
));
1243 httpversionout
= pyhttpverinfo
[0];
1244 httpmethodout
= httpmethod
;
1245 httpurlout
= geturls_text
.getinfo(geturls_text
.EFFECTIVE_URL
);
1246 httpheaderout
= pycurlheadersout
;
1247 httpheadersentout
= httpheaders
;
1248 elif(httplibuse
=="ftp"):
1249 geturls_text
= download_file_from_ftp_file(httpurl
);
1250 if(not geturls_text
):
1252 downloadsize
= None;
1253 if(downloadsize
is not None):
1254 downloadsize
= int(downloadsize
);
1255 if downloadsize
is None: downloadsize
= 0;
1258 log
.info("Downloading URL "+httpurl
);
1259 with
BytesIO() as strbuf
:
1261 databytes
= geturls_text
.read(buffersize
);
1262 if not databytes
: break;
1263 datasize
= len(databytes
);
1264 fulldatasize
= datasize
+ fulldatasize
;
1267 percentage
= str("{0:.2f}".format(float(float(fulldatasize
/ downloadsize
) * 100))).rstrip('0').rstrip('.')+"%";
1268 downloaddiff
= fulldatasize
- prevdownsize
;
1269 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']);
1270 prevdownsize
= fulldatasize
;
1271 strbuf
.write(databytes
);
1273 returnval_content
= strbuf
.read();
1274 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};
1275 geturls_text
.close();
1276 elif(httplibuse
=="sftp"):
1277 geturls_text
= download_file_from_sftp_file(httpurl
);
1278 if(not geturls_text
):
1280 downloadsize
= None;
1281 if(downloadsize
is not None):
1282 downloadsize
= int(downloadsize
);
1283 if downloadsize
is None: downloadsize
= 0;
1286 log
.info("Downloading URL "+httpurl
);
1287 with
BytesIO() as strbuf
:
1289 databytes
= geturls_text
.read(buffersize
);
1290 if not databytes
: break;
1291 datasize
= len(databytes
);
1292 fulldatasize
= datasize
+ fulldatasize
;
1295 percentage
= str("{0:.2f}".format(float(float(fulldatasize
/ downloadsize
) * 100))).rstrip('0').rstrip('.')+"%";
1296 downloaddiff
= fulldatasize
- prevdownsize
;
1297 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']);
1298 prevdownsize
= fulldatasize
;
1299 strbuf
.write(databytes
);
1301 returnval_content
= strbuf
.read();
1302 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};
1303 geturls_text
.close();
1305 elif(httplibuse
=="pysftp"):
1306 geturls_text
= download_file_from_pysftp_file(httpurl
);
1307 if(not geturls_text
):
1309 downloadsize
= None;
1310 if(downloadsize
is not None):
1311 downloadsize
= int(downloadsize
);
1312 if downloadsize
is None: downloadsize
= 0;
1315 log
.info("Downloading URL "+httpurl
);
1316 with
BytesIO() as strbuf
:
1318 databytes
= geturls_text
.read(buffersize
);
1319 if not databytes
: break;
1320 datasize
= len(databytes
);
1321 fulldatasize
= datasize
+ fulldatasize
;
1324 percentage
= str("{0:.2f}".format(float(float(fulldatasize
/ downloadsize
) * 100))).rstrip('0').rstrip('.')+"%";
1325 downloaddiff
= fulldatasize
- prevdownsize
;
1326 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']);
1327 prevdownsize
= fulldatasize
;
1328 strbuf
.write(databytes
);
1330 returnval_content
= strbuf
.read();
1331 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};
1332 geturls_text
.close();
1336 if(isinstance(httpheaderout
, list) and (httplibuse
!="pycurl" and httplibuse
!="pycurl2" and httplibuse
!="pycurl3")):
1337 httpheaderout
= dict(make_http_headers_from_list_to_dict(httpheaderout
));
1338 if(isinstance(httpheaderout
, list) and (httplibuse
=="pycurl" or httplibuse
=="pycurl2" or httplibuse
=="pycurl3")):
1339 httpheaderout
= dict(make_http_headers_from_pycurl_to_dict("\r\n".join(httpheaderout
)));
1340 if(sys
.version
[0]=="2"):
1342 prehttpheaderout
= httpheaderout
;
1343 httpheaderkeys
= httpheaderout
.keys();
1344 imax
= len(httpheaderkeys
);
1348 httpheaderout
.update({httpheaderkeys
[ic
]: prehttpheaderout
[httpheaderkeys
[ic
]]});
1350 except AttributeError:
1352 httpheaderout
= fix_header_names(httpheaderout
);
1353 if(isinstance(httpheadersentout
, list) and (httplibuse
!="pycurl" and httplibuse
!="pycurl2" and httplibuse
!="pycurl3")):
1354 httpheadersentout
= dict(make_http_headers_from_list_to_dict(httpheadersentout
));
1355 if(isinstance(httpheadersentout
, list) and (httplibuse
=="pycurl" or httplibuse
=="pycurl2" or httplibuse
=="pycurl3")):
1356 httpheadersentout
= dict(make_http_headers_from_pycurl_to_dict("\r\n".join(httpheadersentout
)));
1357 httpheadersentout
= fix_header_names(httpheadersentout
);
1358 log
.info("Downloading URL "+httpurl
);
1359 if(httplibuse
=="urllib" or httplibuse
=="request" or httplibuse
=="request3" or httplibuse
=="aiohttp" or httplibuse
=="httplib" or httplibuse
=="httplib2" or httplibuse
=="urllib3" or httplibuse
=="mechanize"):
1360 downloadsize
= httpheaderout
.get('Content-Length');
1361 if(downloadsize
is not None):
1362 downloadsize
= int(downloadsize
);
1363 if downloadsize
is None: downloadsize
= 0;
1366 log
.info("Downloading URL "+httpurl
);
1367 with
BytesIO() as strbuf
:
1369 databytes
= geturls_text
.read(buffersize
);
1370 if not databytes
: break;
1371 datasize
= len(databytes
);
1372 fulldatasize
= datasize
+ fulldatasize
;
1375 percentage
= str("{0:.2f}".format(float(float(fulldatasize
/ downloadsize
) * 100))).rstrip('0').rstrip('.')+"%";
1376 downloaddiff
= fulldatasize
- prevdownsize
;
1377 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']);
1378 prevdownsize
= fulldatasize
;
1379 strbuf
.write(databytes
);
1381 returnval_content
= strbuf
.read();
1382 geturls_text
.close();
1383 if(httpheaderout
.get("Content-Encoding")=="gzip"):
1385 returnval_content
= zlib
.decompress(returnval_content
, 16+zlib
.MAX_WBITS
);
1388 elif(httpheaderout
.get("Content-Encoding")=="deflate"):
1390 returnval_content
= zlib
.decompress(returnval_content
);
1393 elif(httpheaderout
.get("Content-Encoding")=="br" and havebrotli
):
1395 returnval_content
= brotli
.decompress(returnval_content
);
1396 except brotli
.error
:
1398 elif(httpheaderout
.get("Content-Encoding")=="zstd" and havezstd
):
1400 returnval_content
= zstandard
.decompress(returnval_content
);
1401 except zstandard
.error
:
1403 elif((httpheaderout
.get("Content-Encoding")=="lzma" or httpheaderout
.get("Content-Encoding")=="xz") and havelzma
):
1405 returnval_content
= lzma
.decompress(returnval_content
);
1406 except zstandard
.error
:
1408 elif(httpheaderout
.get("Content-Encoding")=="bzip2"):
1410 returnval_content
= bz2
.decompress(returnval_content
);
1411 except zstandard
.error
:
1413 elif(httplibuse
=="httpx" or httplibuse
=="httpx2" or httplibuse
=="httpcore" or httplibuse
=="httpcore2"):
1414 downloadsize
= httpheaderout
.get('Content-Length');
1415 if(downloadsize
is not None):
1416 downloadsize
= int(downloadsize
);
1417 if downloadsize
is None: downloadsize
= 0;
1420 log
.info("Downloading URL "+httpurl
);
1421 with
BytesIO() as strbuf
:
1423 databytes
= geturls_text
.read();
1424 if not databytes
: break;
1425 datasize
= len(databytes
);
1426 fulldatasize
= datasize
+ fulldatasize
;
1429 percentage
= str("{0:.2f}".format(float(float(fulldatasize
/ downloadsize
) * 100))).rstrip('0').rstrip('.')+"%";
1430 downloaddiff
= fulldatasize
- prevdownsize
;
1431 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']);
1432 prevdownsize
= fulldatasize
;
1433 strbuf
.write(databytes
);
1436 returnval_content
= strbuf
.read();
1437 geturls_text
.close();
1438 if(httpheaderout
.get("Content-Encoding")=="gzip"):
1440 returnval_content
= zlib
.decompress(returnval_content
, 16+zlib
.MAX_WBITS
);
1443 elif(httpheaderout
.get("Content-Encoding")=="deflate"):
1445 returnval_content
= zlib
.decompress(returnval_content
);
1448 elif(httpheaderout
.get("Content-Encoding")=="br" and havebrotli
):
1450 returnval_content
= brotli
.decompress(returnval_content
);
1451 except brotli
.error
:
1453 elif(httpheaderout
.get("Content-Encoding")=="zstd" and havezstd
):
1455 returnval_content
= zstandard
.decompress(returnval_content
);
1456 except zstandard
.error
:
1458 elif((httpheaderout
.get("Content-Encoding")=="lzma" or httpheaderout
.get("Content-Encoding")=="xz") and havelzma
):
1460 returnval_content
= lzma
.decompress(returnval_content
);
1461 except zstandard
.error
:
1463 elif(httpheaderout
.get("Content-Encoding")=="bzip2"):
1465 returnval_content
= bz2
.decompress(returnval_content
);
1466 except zstandard
.error
:
1468 elif(httplibuse
=="requests"):
1469 log
.info("Downloading URL "+httpurl
);
1470 downloadsize
= httpheaderout
.get('Content-Length');
1471 if(downloadsize
is not None):
1472 downloadsize
= int(downloadsize
);
1473 if downloadsize
is None: downloadsize
= 0;
1476 log
.info("Downloading URL "+httpurl
);
1477 with
BytesIO() as strbuf
:
1479 databytes
= geturls_text
.raw
.read(buffersize
);
1480 if not databytes
: break;
1481 datasize
= len(databytes
);
1482 fulldatasize
= datasize
+ fulldatasize
;
1485 percentage
= str("{0:.2f}".format(float(float(fulldatasize
/ downloadsize
) * 100))).rstrip('0').rstrip('.')+"%";
1486 downloaddiff
= fulldatasize
- prevdownsize
;
1487 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']);
1488 prevdownsize
= fulldatasize
;
1489 strbuf
.write(databytes
);
1491 returnval_content
= strbuf
.read();
1492 geturls_text
.close();
1493 if(httpheaderout
.get("Content-Encoding")=="gzip"):
1495 returnval_content
= zlib
.decompress(returnval_content
, 16+zlib
.MAX_WBITS
);
1498 elif(httpheaderout
.get("Content-Encoding")=="deflate"):
1500 returnval_content
= zlib
.decompress(returnval_content
);
1503 elif(httpheaderout
.get("Content-Encoding")=="br" and havebrotli
):
1505 returnval_content
= brotli
.decompress(returnval_content
);
1506 except brotli
.error
:
1508 elif(httpheaderout
.get("Content-Encoding")=="zstd" and havezstd
):
1510 returnval_content
= zstandard
.decompress(returnval_content
);
1511 except zstandard
.error
:
1513 elif((httpheaderout
.get("Content-Encoding")=="lzma" or httpheaderout
.get("Content-Encoding")=="xz") and havelzma
):
1515 returnval_content
= lzma
.decompress(returnval_content
);
1516 except zstandard
.error
:
1518 elif(httpheaderout
.get("Content-Encoding")=="bzip2"):
1520 returnval_content
= bz2
.decompress(returnval_content
);
1521 except zstandard
.error
:
1523 elif(httplibuse
=="pycurl" or httplibuse
=="pycurl2" or httplibuse
=="pycurl3"):
1524 log
.info("Downloading URL "+httpurl
);
1525 downloadsize
= httpheaderout
.get('Content-Length');
1526 if(downloadsize
is not None):
1527 downloadsize
= int(downloadsize
);
1528 if downloadsize
is None: downloadsize
= 0;
1531 log
.info("Downloading URL "+httpurl
);
1532 with
BytesIO() as strbuf
:
1534 databytes
= retrieved_body
.read(buffersize
);
1535 if not databytes
: break;
1536 datasize
= len(databytes
);
1537 fulldatasize
= datasize
+ fulldatasize
;
1540 percentage
= str("{0:.2f}".format(float(float(fulldatasize
/ downloadsize
) * 100))).rstrip('0').rstrip('.')+"%";
1541 downloaddiff
= fulldatasize
- prevdownsize
;
1542 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']);
1543 prevdownsize
= fulldatasize
;
1544 strbuf
.write(databytes
);
1546 returnval_content
= strbuf
.read();
1547 geturls_text
.close();
1548 if(httpheaderout
.get("Content-Encoding")=="gzip"):
1550 returnval_content
= zlib
.decompress(returnval_content
, 16+zlib
.MAX_WBITS
);
1553 elif(httpheaderout
.get("Content-Encoding")=="deflate"):
1555 returnval_content
= zlib
.decompress(returnval_content
);
1558 elif(httpheaderout
.get("Content-Encoding")=="br" and havebrotli
):
1560 returnval_content
= brotli
.decompress(returnval_content
);
1561 except brotli
.error
:
1563 elif(httpheaderout
.get("Content-Encoding")=="zstd" and havezstd
):
1565 returnval_content
= zstandard
.decompress(returnval_content
);
1566 except zstandard
.error
:
1568 elif((httpheaderout
.get("Content-Encoding")=="lzma" or httpheaderout
.get("Content-Encoding")=="xz") and havelzma
):
1570 returnval_content
= lzma
.decompress(returnval_content
);
1571 except zstandard
.error
:
1573 elif(httpheaderout
.get("Content-Encoding")=="bzip2"):
1575 returnval_content
= bz2
.decompress(returnval_content
);
1576 except zstandard
.error
:
1578 elif(httplibuse
=="ftp" or httplibuse
=="sftp" or httplibuse
=="pysftp"):
1582 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
};
1585 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):
1586 if(isinstance(httpurl
, list)):
1588 elif(isinstance(httpurl
, tuple)):
1590 elif(isinstance(httpurl
, dict)):
1591 httpurl
= httpurl
.values();
1593 httpurl
= [httpurl
];
1594 listsize
= len(httpurl
);
1597 while(listcount
<listsize
):
1598 ouputval
= download_from_url(httpurl
[listcount
], httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, httplibuse
, buffersize
, sleep
, timeout
);
1599 returnval
.append(ouputval
);
1603 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):
1604 global geturls_download_sleep
, havezstd
, havebrotli
, tmpfileprefix
, tmpfilesuffix
, haveaiohttp
, haverequests
, havemechanize
, havehttplib2
, haveurllib3
, havehttpx
, havehttpcore
, haveparamiko
, havepysftp
;
1605 exec_time_start
= time
.time();
1606 myhash
= hashlib
.new("sha1");
1607 if(sys
.version
[0]=="2"):
1608 myhash
.update(httpurl
);
1609 myhash
.update(str(buffersize
));
1610 myhash
.update(str(exec_time_start
));
1611 if(sys
.version
[0]>="3"):
1612 myhash
.update(httpurl
.encode('utf-8'));
1613 myhash
.update(str(buffersize
).encode('utf-8'));
1614 myhash
.update(str(exec_time_start
).encode('utf-8'));
1615 newtmpfilesuffix
= tmpfilesuffix
+ str(myhash
.hexdigest());
1617 sleep
= geturls_download_sleep
;
1620 if(httplibuse
=="urllib1" or httplibuse
=="urllib2" or httplibuse
=="request"):
1621 httplibuse
= "urllib";
1622 if(httplibuse
=="httplib1"):
1623 httplibuse
= "httplib";
1624 if(not haverequests
and httplibuse
=="requests"):
1625 httplibuse
= "urllib";
1626 if(not haveaiohttp
and httplibuse
=="aiohttp"):
1627 httplibuse
= "urllib";
1628 if(not havehttpx
and httplibuse
=="httpx"):
1629 httplibuse
= "urllib";
1630 if(not havehttpx
and httplibuse
=="httpx2"):
1631 httplibuse
= "urllib";
1632 if(not havehttpcore
and httplibuse
=="httpcore"):
1633 httplibuse
= "urllib";
1634 if(not havehttpcore
and httplibuse
=="httpcore2"):
1635 httplibuse
= "urllib";
1636 if(not havemechanize
and httplibuse
=="mechanize"):
1637 httplibuse
= "urllib";
1638 if(not havepycurl
and httplibuse
=="pycurl"):
1639 httplibuse
= "urllib";
1640 if(not havepycurl
and httplibuse
=="pycurl2"):
1641 httplibuse
= "urllib";
1642 if(havepycurl
and httplibuse
=="pycurl2" and not hasattr(pycurl
, "CURL_HTTP_VERSION_2_0")):
1643 httplibuse
= "pycurl";
1644 if(not havepycurl
and httplibuse
=="pycurl3"):
1645 httplibuse
= "urllib";
1646 if(havepycurl
and httplibuse
=="pycurl3" and not hasattr(pycurl
, "CURL_HTTP_VERSION_3_0") and hasattr(pycurl
, "CURL_HTTP_VERSION_2_0")):
1647 httplibuse
= "pycurl2";
1648 if(havepycurl
and httplibuse
=="pycurl3" and not hasattr(pycurl
, "CURL_HTTP_VERSION_3_0") and not hasattr(pycurl
, "CURL_HTTP_VERSION_2_0")):
1649 httplibuse
= "pycurl";
1650 if(not havehttplib2
and httplibuse
=="httplib2"):
1651 httplibuse
= "httplib";
1652 if(not haveparamiko
and httplibuse
=="sftp"):
1654 if(not haveparamiko
and httplibuse
=="pysftp"):
1656 pretmpfilename
= download_from_url(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, httplibuse
, buffersize
, sleep
, timeout
);
1657 if(not pretmpfilename
):
1659 with tempfile
.NamedTemporaryFile('wb+', prefix
=tmpfileprefix
, suffix
=newtmpfilesuffix
, delete
=False) as f
:
1660 tmpfilename
= f
.name
;
1662 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())));
1663 except AttributeError:
1665 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())));
1670 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')};
1671 f
.write(pretmpfilename
.get('Content'));
1673 exec_time_end
= time
.time();
1674 log
.info("It took "+hms_string(exec_time_start
- exec_time_end
)+" to download file.");
1675 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
)});
1678 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):
1679 if(isinstance(httpurl
, list)):
1681 elif(isinstance(httpurl
, tuple)):
1683 elif(isinstance(httpurl
, dict)):
1684 httpurl
= httpurl
.values();
1686 httpurl
= [httpurl
];
1687 listsize
= len(httpurl
);
1690 while(listcount
<listsize
):
1691 ouputval
= download_from_url_file(httpurl
[listcount
], httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, httplibuse
, ranges
, buffersize
, sleep
, timeout
);
1692 returnval
.append(ouputval
);
1696 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):
1697 global geturls_download_sleep
, havezstd
, havebrotli
, haveaiohttp
, haverequests
, havemechanize
, havepycurl
, havehttplib2
, haveurllib3
, havehttpx
, havehttpcore
, haveparamiko
, havepysftp
;
1699 sleep
= geturls_download_sleep
;
1702 if(httplibuse
=="urllib1" or httplibuse
=="urllib2" or httplibuse
=="request"):
1703 httplibuse
= "urllib";
1704 if(httplibuse
=="httplib1"):
1705 httplibuse
= "httplib";
1706 if(not haverequests
and httplibuse
=="requests"):
1707 httplibuse
= "urllib";
1708 if(not haveaiohttp
and httplibuse
=="aiohttp"):
1709 httplibuse
= "urllib";
1710 if(not havehttpx
and httplibuse
=="httpx"):
1711 httplibuse
= "urllib";
1712 if(not havehttpx
and httplibuse
=="httpx2"):
1713 httplibuse
= "urllib";
1714 if(not havehttpcore
and httplibuse
=="httpcore"):
1715 httplibuse
= "urllib";
1716 if(not havehttpcore
and httplibuse
=="httpcore2"):
1717 httplibuse
= "urllib";
1718 if(not havemechanize
and httplibuse
=="mechanize"):
1719 httplibuse
= "urllib";
1720 if(not havepycurl
and httplibuse
=="pycurl"):
1721 httplibuse
= "urllib";
1722 if(not havepycurl
and httplibuse
=="pycurl2"):
1723 httplibuse
= "urllib";
1724 if(havepycurl
and httplibuse
=="pycurl2" and not hasattr(pycurl
, "CURL_HTTP_VERSION_2_0")):
1725 httplibuse
= "pycurl";
1726 if(not havepycurl
and httplibuse
=="pycurl3"):
1727 httplibuse
= "urllib";
1728 if(havepycurl
and httplibuse
=="pycurl3" and not hasattr(pycurl
, "CURL_HTTP_VERSION_3_0") and hasattr(pycurl
, "CURL_HTTP_VERSION_2_0")):
1729 httplibuse
= "pycurl2";
1730 if(havepycurl
and httplibuse
=="pycurl3" and not hasattr(pycurl
, "CURL_HTTP_VERSION_3_0") and not hasattr(pycurl
, "CURL_HTTP_VERSION_2_0")):
1731 httplibuse
= "pycurl";
1732 if(not havehttplib2
and httplibuse
=="httplib2"):
1733 httplibuse
= "httplib";
1734 if(not haveparamiko
and httplibuse
=="sftp"):
1736 if(not havepysftp
and httplibuse
=="pysftp"):
1738 if(not outfile
=="-"):
1739 outpath
= outpath
.rstrip(os
.path
.sep
);
1740 filepath
= os
.path
.realpath(outpath
+os
.path
.sep
+outfile
);
1741 if(not os
.path
.exists(outpath
)):
1742 os
.makedirs(outpath
);
1743 if(os
.path
.exists(outpath
) and os
.path
.isfile(outpath
)):
1745 if(os
.path
.exists(filepath
) and os
.path
.isdir(filepath
)):
1747 pretmpfilename
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, httplibuse
, ranges
, buffersize
[0], sleep
, timeout
);
1748 if(not pretmpfilename
):
1750 tmpfilename
= pretmpfilename
.get('Filename');
1751 downloadsize
= int(os
.path
.getsize(tmpfilename
));
1753 log
.info("Moving file "+tmpfilename
+" to "+filepath
);
1754 exec_time_start
= time
.time();
1755 shutil
.move(tmpfilename
, filepath
);
1757 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())));
1758 except AttributeError:
1760 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())));
1765 exec_time_end
= time
.time();
1766 log
.info("It took "+hms_string(exec_time_start
- exec_time_end
)+" to move file.");
1767 if(os
.path
.exists(tmpfilename
)):
1768 os
.remove(tmpfilename
);
1769 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')};
1771 pretmpfilename
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, httplibuse
, ranges
, buffersize
[0], sleep
, timeout
);
1772 tmpfilename
= pretmpfilename
.get('Filename');
1773 downloadsize
= int(os
.path
.getsize(tmpfilename
));
1776 exec_time_start
= time
.time();
1777 with
open(tmpfilename
, 'rb') as ft
:
1780 databytes
= ft
.read(buffersize
[1]);
1781 if not databytes
: break;
1782 datasize
= len(databytes
);
1783 fulldatasize
= datasize
+ fulldatasize
;
1786 percentage
= str("{0:.2f}".format(float(float(fulldatasize
/ downloadsize
) * 100))).rstrip('0').rstrip('.')+"%";
1787 downloaddiff
= fulldatasize
- prevdownsize
;
1788 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']);
1789 prevdownsize
= fulldatasize
;
1792 fdata
= f
.getvalue();
1795 os
.remove(tmpfilename
);
1796 exec_time_end
= time
.time();
1797 log
.info("It took "+hms_string(exec_time_start
- exec_time_end
)+" to copy file.");
1798 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
};
1801 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):
1802 if(isinstance(httpurl
, list)):
1804 elif(isinstance(httpurl
, tuple)):
1806 elif(isinstance(httpurl
, dict)):
1807 httpurl
= httpurl
.values();
1809 httpurl
= [httpurl
];
1810 listsize
= len(httpurl
);
1813 while(listcount
<listsize
):
1814 ouputval
= download_from_url_to_file(httpurl
[listcount
], httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, httplibuse
, outfile
, outpath
, ranges
, buffersize
, sleep
, timeout
);
1815 returnval
.append(ouputval
);
1819 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):
1820 returnval
= download_from_url(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "urllib", buffersize
, sleep
, timeout
);
1823 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):
1824 returnval
= download_from_url(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "urllib", buffersize
, sleep
, timeout
);
1827 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):
1828 returnval
= download_from_url(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "request3", buffersize
, sleep
, timeout
);
1831 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):
1832 returnval
= download_from_url(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "httplib", buffersize
, sleep
, timeout
);
1835 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):
1836 returnval
= download_from_url(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "httplib2", buffersize
, sleep
, timeout
);
1839 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):
1840 returnval
= download_from_url(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "urllib3", buffersize
, sleep
, timeout
);
1843 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):
1844 returnval
= download_from_url(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "requests", buffersize
, sleep
, timeout
);
1847 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):
1848 returnval
= download_from_url(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "aiohttp", buffersize
, sleep
, timeout
);
1851 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):
1852 returnval
= download_from_url(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "httpx", buffersize
, sleep
, timeout
);
1855 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):
1856 returnval
= download_from_url(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "httpx2", buffersize
, sleep
, timeout
);
1859 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):
1860 returnval
= download_from_url(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "httpcore", buffersize
, sleep
, timeout
);
1863 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):
1864 returnval
= download_from_url(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "httpcore2", buffersize
, sleep
, timeout
);
1867 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):
1868 returnval
= download_from_url(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "mechanize", buffersize
, sleep
, timeout
);
1871 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):
1872 returnval
= download_from_url(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "pycurl", buffersize
, sleep
, timeout
);
1875 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):
1876 returnval
= download_from_url(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "pycurl2", buffersize
, sleep
, timeout
);
1879 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):
1880 returnval
= download_from_url(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "pycurl3", buffersize
, sleep
, timeout
);
1883 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):
1884 returnval
= download_from_url(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "ftp", buffersize
, sleep
, timeout
);
1887 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):
1888 returnval
= download_from_url(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "sftp", buffersize
, sleep
, timeout
);
1891 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):
1892 returnval
= download_from_url(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "pysftp", buffersize
, sleep
, timeout
);
1895 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):
1896 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "urllib", ranges
, buffersize
, sleep
, timeout
);
1899 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):
1900 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "urllib", ranges
, buffersize
, sleep
, timeout
);
1903 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):
1904 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "request3", ranges
, buffersize
, sleep
, timeout
);
1907 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):
1908 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "httplib", ranges
, buffersize
, sleep
, timeout
);
1911 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):
1912 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "httplib2", ranges
, buffersize
, sleep
, timeout
);
1915 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):
1916 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "urllib3", ranges
, buffersize
, sleep
, timeout
);
1919 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):
1920 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "requests", ranges
, buffersize
, sleep
, timeout
);
1923 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):
1924 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "aiohttp", ranges
, buffersize
, sleep
, timeout
);
1927 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):
1928 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "httpx", ranges
, buffersize
, sleep
, timeout
);
1931 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):
1932 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "httpx2", ranges
, buffersize
, sleep
, timeout
);
1935 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):
1936 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "httpcore", ranges
, buffersize
, sleep
, timeout
);
1939 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):
1940 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "httpcore2", ranges
, buffersize
, sleep
, timeout
);
1943 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):
1944 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "mechanize", ranges
, buffersize
, sleep
, timeout
);
1947 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):
1948 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "pycurl", ranges
, buffersize
, sleep
, timeout
);
1951 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):
1952 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "pycurl2", ranges
, buffersize
, sleep
, timeout
);
1955 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):
1956 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "pycurl3", ranges
, buffersize
, sleep
, timeout
);
1959 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):
1960 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "ftp", ranges
, buffersize
, sleep
, timeout
);
1963 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):
1964 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "sftp", ranges
, buffersize
, sleep
, timeout
);
1967 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):
1968 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "pysftp", ranges
, buffersize
, sleep
, timeout
);
1971 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):
1972 returnval
= download_from_url_to_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "urllib", outfile
, outpath
, ranges
, buffersize
, sleep
, timeout
);
1975 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):
1976 returnval
= download_from_url_to_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "request", outfile
, outpath
, ranges
, buffersize
, sleep
, timeout
);
1979 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):
1980 returnval
= download_from_url_to_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "urllib", outfile
, outpath
, ranges
, buffersize
, sleep
, timeout
);
1983 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):
1984 returnval
= download_from_url_to_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "httplib", outfile
, outpath
, ranges
, buffersize
, sleep
, timeout
);
1987 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):
1988 returnval
= download_from_url_to_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "httplib2", outfile
, outpath
, ranges
, buffersize
, sleep
, timeout
);
1991 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):
1992 returnval
= download_from_url_to_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "urllib3", outfile
, outpath
, ranges
, buffersize
, sleep
, timeout
);
1995 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):
1996 returnval
= download_from_url_to_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "requests", outfile
, outpath
, ranges
, buffersize
, sleep
, timeout
);
1999 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):
2000 returnval
= download_from_url_to_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "aiohttp", outfile
, outpath
, ranges
, buffersize
, sleep
, timeout
);
2003 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):
2004 returnval
= download_from_url_to_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "httpx", outfile
, outpath
, ranges
, buffersize
, sleep
, timeout
);
2007 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):
2008 returnval
= download_from_url_to_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "httpx2", outfile
, outpath
, ranges
, buffersize
, sleep
, timeout
);
2011 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):
2012 returnval
= download_from_url_to_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "httpcore", outfile
, outpath
, ranges
, buffersize
, sleep
, timeout
);
2015 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):
2016 returnval
= download_from_url_to_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "httpcore2", outfile
, outpath
, ranges
, buffersize
, sleep
, timeout
);
2019 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):
2020 returnval
= download_from_url_to_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "mechanize", outfile
, outpath
, ranges
, buffersize
, sleep
, timeout
);
2023 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):
2024 returnval
= download_from_url_to_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "pycurl", outfile
, outpath
, ranges
, buffersize
, sleep
, timeout
);
2027 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):
2028 returnval
= download_from_url_to_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "pycurl2", outfile
, outpath
, ranges
, buffersize
, sleep
, timeout
);
2031 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):
2032 returnval
= download_from_url_to_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "pycurl3", outfile
, outpath
, ranges
, buffersize
, sleep
, timeout
);
2035 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):
2036 returnval
= download_from_url_to_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "ftp", outfile
, outpath
, ranges
, buffersize
, sleep
, timeout
);
2039 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):
2040 returnval
= download_from_url_to_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "sftp", outfile
, outpath
, ranges
, buffersize
, sleep
, timeout
);
2043 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):
2044 returnval
= download_from_url_to_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "pysftp", outfile
, outpath
, ranges
, buffersize
, sleep
, timeout
);
2047 def download_file_from_ftp_file(url
):
2048 urlparts
= urlparse
.urlparse(url
);
2049 file_name
= os
.path
.basename(urlparts
.path
);
2050 file_dir
= os
.path
.dirname(urlparts
.path
);
2051 if(urlparts
.username
is not None):
2052 ftp_username
= urlparts
.username
;
2054 ftp_username
= "anonymous";
2055 if(urlparts
.password
is not None):
2056 ftp_password
= urlparts
.password
;
2057 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
2058 ftp_password
= "anonymous";
2061 if(urlparts
.scheme
=="ftp"):
2063 elif(urlparts
.scheme
=="ftps"):
2067 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
2069 ftp_port
= urlparts
.port
;
2070 if(urlparts
.port
is None):
2073 ftp
.connect(urlparts
.hostname
, ftp_port
);
2074 except socket
.gaierror
:
2075 log
.info("Error With URL "+httpurl
);
2077 except socket
.timeout
:
2078 log
.info("Error With URL "+httpurl
);
2080 ftp
.login(urlparts
.username
, urlparts
.password
);
2081 if(urlparts
.scheme
=="ftps"):
2083 ftpfile
= BytesIO();
2084 ftp
.retrbinary("RETR "+urlparts
.path
, ftpfile
.write
);
2085 #ftp.storbinary("STOR "+urlparts.path, ftpfile.write);
2090 def download_file_from_ftp_string(url
):
2091 ftpfile
= download_file_from_ftp_file(url
);
2092 return ftpfile
.read();
2094 def upload_file_to_ftp_file(ftpfile
, url
):
2095 urlparts
= urlparse
.urlparse(url
);
2096 file_name
= os
.path
.basename(urlparts
.path
);
2097 file_dir
= os
.path
.dirname(urlparts
.path
);
2098 if(urlparts
.username
is not None):
2099 ftp_username
= urlparts
.username
;
2101 ftp_username
= "anonymous";
2102 if(urlparts
.password
is not None):
2103 ftp_password
= urlparts
.password
;
2104 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
2105 ftp_password
= "anonymous";
2108 if(urlparts
.scheme
=="ftp"):
2110 elif(urlparts
.scheme
=="ftps"):
2114 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
2116 ftp_port
= urlparts
.port
;
2117 if(urlparts
.port
is None):
2120 ftp
.connect(urlparts
.hostname
, ftp_port
);
2121 except socket
.gaierror
:
2122 log
.info("Error With URL "+httpurl
);
2124 except socket
.timeout
:
2125 log
.info("Error With URL "+httpurl
);
2127 ftp
.login(urlparts
.username
, urlparts
.password
);
2128 if(urlparts
.scheme
=="ftps"):
2130 ftp
.storbinary("STOR "+urlparts
.path
, ftpfile
);
2135 def upload_file_to_ftp_string(ftpstring
, url
):
2136 ftpfileo
= BytesIO(ftpstring
);
2137 ftpfile
= upload_file_to_ftp_file(ftpfileo
, url
);
2142 def download_file_from_sftp_file(url
):
2143 urlparts
= urlparse
.urlparse(url
);
2144 file_name
= os
.path
.basename(urlparts
.path
);
2145 file_dir
= os
.path
.dirname(urlparts
.path
);
2146 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
2148 sftp_port
= urlparts
.port
;
2149 if(urlparts
.port
is None):
2152 sftp_port
= urlparts
.port
;
2153 if(urlparts
.username
is not None):
2154 sftp_username
= urlparts
.username
;
2156 sftp_username
= "anonymous";
2157 if(urlparts
.password
is not None):
2158 sftp_password
= urlparts
.password
;
2159 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
2160 sftp_password
= "anonymous";
2163 if(urlparts
.scheme
!="sftp"):
2165 ssh
= paramiko
.SSHClient();
2166 ssh
.load_system_host_keys();
2167 ssh
.set_missing_host_key_policy(paramiko
.AutoAddPolicy());
2169 ssh
.connect(urlparts
.hostname
, port
=sftp_port
, username
=urlparts
.username
, password
=urlparts
.password
);
2170 except paramiko
.ssh_exception
.SSHException
:
2172 except socket
.gaierror
:
2173 log
.info("Error With URL "+httpurl
);
2175 except socket
.timeout
:
2176 log
.info("Error With URL "+httpurl
);
2178 sftp
= ssh
.open_sftp();
2179 sftpfile
= BytesIO();
2180 sftp
.getfo(urlparts
.path
, sftpfile
);
2183 sftpfile
.seek(0, 0);
2186 def download_file_from_sftp_file(url
):
2190 def download_file_from_sftp_string(url
):
2191 sftpfile
= download_file_from_sftp_file(url
);
2192 return sftpfile
.read();
2194 def download_file_from_ftp_string(url
):
2198 def upload_file_to_sftp_file(sftpfile
, url
):
2199 urlparts
= urlparse
.urlparse(url
);
2200 file_name
= os
.path
.basename(urlparts
.path
);
2201 file_dir
= os
.path
.dirname(urlparts
.path
);
2202 sftp_port
= urlparts
.port
;
2203 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
2205 if(urlparts
.port
is None):
2208 sftp_port
= urlparts
.port
;
2209 if(urlparts
.username
is not None):
2210 sftp_username
= urlparts
.username
;
2212 sftp_username
= "anonymous";
2213 if(urlparts
.password
is not None):
2214 sftp_password
= urlparts
.password
;
2215 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
2216 sftp_password
= "anonymous";
2219 if(urlparts
.scheme
!="sftp"):
2221 ssh
= paramiko
.SSHClient();
2222 ssh
.load_system_host_keys();
2223 ssh
.set_missing_host_key_policy(paramiko
.AutoAddPolicy());
2225 ssh
.connect(urlparts
.hostname
, port
=sftp_port
, username
=urlparts
.username
, password
=urlparts
.password
);
2226 except paramiko
.ssh_exception
.SSHException
:
2228 except socket
.gaierror
:
2229 log
.info("Error With URL "+httpurl
);
2231 except socket
.timeout
:
2232 log
.info("Error With URL "+httpurl
);
2234 sftp
= ssh
.open_sftp();
2235 sftp
.putfo(sftpfile
, urlparts
.path
);
2238 sftpfile
.seek(0, 0);
2241 def upload_file_to_sftp_file(sftpfile
, url
):
2245 def upload_file_to_sftp_string(sftpstring
, url
):
2246 sftpfileo
= BytesIO(sftpstring
);
2247 sftpfile
= upload_file_to_sftp_files(ftpfileo
, url
);
2251 def upload_file_to_sftp_string(url
):
2256 def download_file_from_pysftp_file(url
):
2257 urlparts
= urlparse
.urlparse(url
);
2258 file_name
= os
.path
.basename(urlparts
.path
);
2259 file_dir
= os
.path
.dirname(urlparts
.path
);
2260 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
2262 sftp_port
= urlparts
.port
;
2263 if(urlparts
.port
is None):
2266 sftp_port
= urlparts
.port
;
2267 if(urlparts
.username
is not None):
2268 sftp_username
= urlparts
.username
;
2270 sftp_username
= "anonymous";
2271 if(urlparts
.password
is not None):
2272 sftp_password
= urlparts
.password
;
2273 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
2274 sftp_password
= "anonymous";
2277 if(urlparts
.scheme
!="sftp"):
2280 pysftp
.Connection(urlparts
.hostname
, port
=sftp_port
, username
=urlparts
.username
, password
=urlparts
.password
);
2281 except paramiko
.ssh_exception
.SSHException
:
2283 except socket
.gaierror
:
2284 log
.info("Error With URL "+httpurl
);
2286 except socket
.timeout
:
2287 log
.info("Error With URL "+httpurl
);
2289 sftp
= ssh
.open_sftp();
2290 sftpfile
= BytesIO();
2291 sftp
.getfo(urlparts
.path
, sftpfile
);
2294 sftpfile
.seek(0, 0);
2297 def download_file_from_pysftp_file(url
):
2301 def download_file_from_pysftp_string(url
):
2302 sftpfile
= download_file_from_pysftp_file(url
);
2303 return sftpfile
.read();
2305 def download_file_from_ftp_string(url
):
2309 def upload_file_to_pysftp_file(sftpfile
, url
):
2310 urlparts
= urlparse
.urlparse(url
);
2311 file_name
= os
.path
.basename(urlparts
.path
);
2312 file_dir
= os
.path
.dirname(urlparts
.path
);
2313 sftp_port
= urlparts
.port
;
2314 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
2316 if(urlparts
.port
is None):
2319 sftp_port
= urlparts
.port
;
2320 if(urlparts
.username
is not None):
2321 sftp_username
= urlparts
.username
;
2323 sftp_username
= "anonymous";
2324 if(urlparts
.password
is not None):
2325 sftp_password
= urlparts
.password
;
2326 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
2327 sftp_password
= "anonymous";
2330 if(urlparts
.scheme
!="sftp"):
2333 pysftp
.Connection(urlparts
.hostname
, port
=sftp_port
, username
=urlparts
.username
, password
=urlparts
.password
);
2334 except paramiko
.ssh_exception
.SSHException
:
2336 except socket
.gaierror
:
2337 log
.info("Error With URL "+httpurl
);
2339 except socket
.timeout
:
2340 log
.info("Error With URL "+httpurl
);
2342 sftp
= ssh
.open_sftp();
2343 sftp
.putfo(sftpfile
, urlparts
.path
);
2346 sftpfile
.seek(0, 0);
2349 def upload_file_to_pysftp_file(sftpfile
, url
):
2353 def upload_file_to_pysftp_string(sftpstring
, url
):
2354 sftpfileo
= BytesIO(sftpstring
);
2355 sftpfile
= upload_file_to_pysftp_files(ftpfileo
, url
);
2359 def upload_file_to_pysftp_string(url
):