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: 9/24/2023 Ver. 1.5.0 RC 1 - Author: cooldude2k $
18 from __future__
import division
, absolute_import
, print_function
;
19 import re
, os
, sys
, hashlib
, shutil
, platform
, tempfile
, urllib
, gzip
, time
, argparse
, cgi
, subprocess
, socket
, email
.utils
, datetime
, time
;
20 import logging
as log
;
21 from ftplib
import FTP
, FTP_TLS
;
22 from base64
import b64encode
;
29 havemechanize
= False;
34 havemechanize
= False;
55 from httplib2
import HTTPConnectionWithTimeout
, HTTPSConnectionWithTimeout
;
83 if(sys
.version
[0]=="2"):
85 from cStringIO
import StringIO
;
87 from StringIO
import StringIO
;
88 # From http://python-future.org/compatible_idioms.html
89 from urlparse
import urlparse
, urlunparse
, urlsplit
, urlunsplit
, urljoin
;
90 from urllib
import urlencode
;
91 from urllib
import urlopen
as urlopenalt
;
92 from urllib2
import urlopen
, Request
, install_opener
, HTTPError
, URLError
, build_opener
, HTTPCookieProcessor
;
93 import urlparse
, cookielib
;
94 from httplib
import HTTPConnection
, HTTPSConnection
;
95 if(sys
.version
[0]>="3"):
96 from io
import StringIO
, BytesIO
;
97 # From http://python-future.org/compatible_idioms.html
98 from urllib
.parse
import urlparse
, urlunparse
, urlsplit
, urlunsplit
, urljoin
, urlencode
;
99 from urllib
.request
import urlopen
, Request
, install_opener
, build_opener
, HTTPCookieProcessor
;
100 from urllib
.error
import HTTPError
, URLError
;
101 import urllib
.parse
as urlparse
;
102 import http
.cookiejar
as cookielib
;
103 from http
.client
import HTTPConnection
, HTTPSConnection
;
105 __program_name__
= "PyWWW-Get";
106 __program_alt_name__
= "PyWWWGet";
107 __program_small_name__
= "wwwget";
108 __project__
= __program_name__
;
109 __project_url__
= "https://github.com/GameMaker2k/PyWWW-Get";
110 __version_info__
= (1, 5, 0, "RC 1", 1);
111 __version_date_info__
= (2023, 9, 24, "RC 1", 1);
112 __version_date__
= str(__version_date_info__
[0])+"."+str(__version_date_info__
[1]).zfill(2)+"."+str(__version_date_info__
[2]).zfill(2);
113 __revision__
= __version_info__
[3];
114 __revision_id__
= "$Id$";
115 if(__version_info__
[4] is not None):
116 __version_date_plusrc__
= __version_date__
+"-"+str(__version_date_info__
[4]);
117 if(__version_info__
[4] is None):
118 __version_date_plusrc__
= __version_date__
;
119 if(__version_info__
[3] is not None):
120 __version__
= str(__version_info__
[0])+"."+str(__version_info__
[1])+"."+str(__version_info__
[2])+" "+str(__version_info__
[3]);
121 if(__version_info__
[3] is None):
122 __version__
= str(__version_info__
[0])+"."+str(__version_info__
[1])+"."+str(__version_info__
[2]);
124 tmpfileprefix
= "py"+str(sys
.version_info
[0])+__program_small_name__
+str(__version_info__
[0])+"-";
126 pytempdir
= tempfile
.gettempdir();
128 PyBitness
= platform
.architecture();
129 if(PyBitness
=="32bit" or PyBitness
=="32"):
131 elif(PyBitness
=="64bit" or PyBitness
=="64"):
136 compression_supported
= "gzip, deflate";
138 compression_supported
= "gzip, deflate, br";
140 compression_supported
= "gzip, deflate";
142 geturls_cj
= cookielib
.CookieJar();
143 windowsNT4_ua_string
= "Windows NT 4.0";
144 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"};
145 windows2k_ua_string
= "Windows NT 5.0";
146 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"};
147 windowsXP_ua_string
= "Windows NT 5.1";
148 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"};
149 windowsXP64_ua_string
= "Windows NT 5.2; Win64; x64";
150 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"};
151 windows7_ua_string
= "Windows NT 6.1; Win64; x64";
152 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"};
153 windows8_ua_string
= "Windows NT 6.2; Win64; x64";
154 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"};
155 windows81_ua_string
= "Windows NT 6.3; Win64; x64";
156 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"};
157 windows10_ua_string
= "Windows NT 10.0; Win64; x64";
158 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"};
159 windows11_ua_string
= "Windows NT 11.0; Win64; x64";
160 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"};
161 geturls_ua_firefox_windows7
= "Mozilla/5.0 ("+windows7_ua_string
+"; rv:109.0) Gecko/20100101 Firefox/117.0";
162 geturls_ua_seamonkey_windows7
= "Mozilla/5.0 ("+windows7_ua_string
+"; rv:91.0) Gecko/20100101 Firefox/91.0 SeaMonkey/2.53.17";
163 geturls_ua_chrome_windows7
= "Mozilla/5.0 ("+windows7_ua_string
+") AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36";
164 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";
165 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";
166 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";
167 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";
168 geturls_ua_internet_explorer_windows7
= "Mozilla/5.0 ("+windows7_ua_string
+"; Trident/7.0; rv:11.0) like Gecko";
169 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";
170 geturls_ua_pywwwget_python
= "Mozilla/5.0 (compatible; {proname}/{prover}; +{prourl})".format(proname
=__project__
, prover
=__version__
, prourl
=__project_url__
);
171 if(platform
.python_implementation()!=""):
172 py_implementation
= platform
.python_implementation();
173 if(platform
.python_implementation()==""):
174 py_implementation
= "Python";
175 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__
);
176 geturls_ua_googlebot_google
= "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)";
177 geturls_ua_googlebot_google_old
= "Googlebot/2.1 (+http://www.google.com/bot.html)";
178 geturls_ua
= geturls_ua_firefox_windows7
;
179 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"};
180 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"};
181 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"};
182 geturls_headers_chrome_windows7
.update(windows7_ua_addon
);
183 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"};
184 geturls_headers_chromium_windows7
.update(windows7_ua_addon
);
185 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"};
186 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"};
187 geturls_headers_opera_windows7
.update(windows7_ua_addon
);
188 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"};
189 geturls_headers_vivaldi_windows7
.update(windows7_ua_addon
);
190 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"};
191 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"}
192 geturls_headers_microsoft_edge_windows7
.update(windows7_ua_addon
);
193 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
)};
194 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
)};
195 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"};
196 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"};
197 geturls_headers
= geturls_headers_firefox_windows7
;
198 geturls_download_sleep
= 0;
200 def verbose_printout(dbgtxt
, outtype
="log", dbgenable
=True, dgblevel
=20):
201 if(outtype
=="print" and dbgenable
):
204 elif(outtype
=="log" and dbgenable
):
205 logging
.info(dbgtxt
);
207 elif(outtype
=="warning" and dbgenable
):
208 logging
.warning(dbgtxt
);
210 elif(outtype
=="error" and dbgenable
):
211 logging
.error(dbgtxt
);
213 elif(outtype
=="critical" and dbgenable
):
214 logging
.critical(dbgtxt
);
216 elif(outtype
=="exception" and dbgenable
):
217 logging
.exception(dbgtxt
);
219 elif(outtype
=="logalt" and dbgenable
):
220 logging
.log(dgblevel
, dbgtxt
);
222 elif(outtype
=="debug" and dbgenable
):
223 logging
.debug(dbgtxt
);
231 def verbose_printout_return(dbgtxt
, outtype
="log", dbgenable
=True, dgblevel
=20):
232 dbgout
= verbose_printout(dbgtxt
, outtype
, dbgenable
, dgblevel
);
237 def add_url_param(url
, **params
):
239 parts
= list(urlparse
.urlsplit(url
));
240 d
= dict(cgi
.parse_qsl(parts
[n
])); # use cgi.parse_qs for list values
242 parts
[n
]=urlencode(d
);
243 return urlparse
.urlunsplit(parts
);
245 os
.environ
["PATH"] = os
.environ
["PATH"] + os
.pathsep
+ os
.path
.dirname(os
.path
.realpath(__file__
)) + os
.pathsep
+ os
.getcwd();
246 def which_exec(execfile):
247 for path
in os
.environ
["PATH"].split(":"):
248 if os
.path
.exists(path
+ "/" + execfile):
249 return path
+ "/" + execfile;
251 def listize(varlist
):
259 newlistreg
.update({ilx
: varlist
[il
]});
260 newlistrev
.update({varlist
[il
]: ilx
});
263 newlistfull
= {1: newlistreg
, 2: newlistrev
, 'reg': newlistreg
, 'rev': newlistrev
};
266 def twolistize(varlist
):
276 newlistnamereg
.update({ilx
: varlist
[il
][0].strip()});
277 newlistnamerev
.update({varlist
[il
][0].strip(): ilx
});
278 newlistdescreg
.update({ilx
: varlist
[il
][1].strip()});
279 newlistdescrev
.update({varlist
[il
][1].strip(): ilx
});
282 newlistnametmp
= {1: newlistnamereg
, 2: newlistnamerev
, 'reg': newlistnamereg
, 'rev': newlistnamerev
};
283 newlistdesctmp
= {1: newlistdescreg
, 2: newlistdescrev
, 'reg': newlistdescreg
, 'rev': newlistdescrev
};
284 newlistfull
= {1: newlistnametmp
, 2: newlistdesctmp
, 'name': newlistnametmp
, 'desc': newlistdesctmp
}
287 def arglistize(proexec
, *varlist
):
291 newarglist
= [proexec
];
293 if varlist
[il
][0] is not None:
294 newarglist
.append(varlist
[il
][0]);
295 if varlist
[il
][1] is not None:
296 newarglist
.append(varlist
[il
][1]);
300 def fix_header_names(header_dict
):
301 header_dict
= {k
.title(): v
for k
, v
in header_dict
.items()};
304 # hms_string by ArcGIS Python Recipes
305 # https://arcpy.wordpress.com/2012/04/20/146/
306 def hms_string(sec_elapsed
):
307 h
= int(sec_elapsed
/ (60 * 60));
308 m
= int((sec_elapsed
% (60 * 60)) / 60);
309 s
= sec_elapsed
% 60.0;
310 return "{}:{:>02}:{:>05.2f}".format(h
, m
, s
);
312 # get_readable_size by Lipis
313 # http://stackoverflow.com/posts/14998888/revisions
314 def get_readable_size(bytes
, precision
=1, unit
="IEC"):
316 if(unit
!="IEC" and unit
!="SI"):
319 units
= [" B"," KiB"," MiB"," GiB"," TiB"," PiB"," EiB"," ZiB"];
320 unitswos
= ["B","KiB","MiB","GiB","TiB","PiB","EiB","ZiB"];
323 units
= [" B"," kB"," MB"," GB"," TB"," PB"," EB"," ZB"];
324 unitswos
= ["B","kB","MB","GB","TB","PB","EB","ZB"];
329 if abs(bytes
) < unitsize
:
330 strformat
= "%3."+str(precision
)+"f%s";
331 pre_return_val
= (strformat
% (bytes
, unit
));
332 pre_return_val
= re
.sub(r
"([0]+) ([A-Za-z]+)", r
" \2", pre_return_val
);
333 pre_return_val
= re
.sub(r
"\. ([A-Za-z]+)", r
" \1", pre_return_val
);
334 alt_return_val
= pre_return_val
.split();
335 return_val
= {'Bytes': orgbytes
, 'ReadableWithSuffix': pre_return_val
, 'ReadableWithoutSuffix': alt_return_val
[0], 'ReadableSuffix': alt_return_val
[1]}
338 strformat
= "%."+str(precision
)+"f%s";
339 pre_return_val
= (strformat
% (bytes
, "YiB"));
340 pre_return_val
= re
.sub(r
"([0]+) ([A-Za-z]+)", r
" \2", pre_return_val
);
341 pre_return_val
= re
.sub(r
"\. ([A-Za-z]+)", r
" \1", pre_return_val
);
342 alt_return_val
= pre_return_val
.split();
343 return_val
= {'Bytes': orgbytes
, 'ReadableWithSuffix': pre_return_val
, 'ReadableWithoutSuffix': alt_return_val
[0], 'ReadableSuffix': alt_return_val
[1]}
346 def get_readable_size_from_file(infile
, precision
=1, unit
="IEC", usehashes
=False, usehashtypes
="md5,sha1"):
348 usehashtypes
= usehashtypes
.lower();
349 getfilesize
= os
.path
.getsize(infile
);
350 return_val
= get_readable_size(getfilesize
, precision
, unit
);
352 hashtypelist
= usehashtypes
.split(",");
353 openfile
= open(infile
, "rb");
354 filecontents
= openfile
.read();
357 listnumend
= len(hashtypelist
);
358 while(listnumcount
< listnumend
):
359 hashtypelistlow
= hashtypelist
[listnumcount
].strip();
360 hashtypelistup
= hashtypelistlow
.upper();
361 filehash
= hashlib
.new(hashtypelistup
);
362 filehash
.update(filecontents
);
363 filegethash
= filehash
.hexdigest();
364 return_val
.update({hashtypelistup
: filegethash
});
368 def get_readable_size_from_string(instring
, precision
=1, unit
="IEC", usehashes
=False, usehashtypes
="md5,sha1"):
370 usehashtypes
= usehashtypes
.lower();
371 getfilesize
= len(instring
);
372 return_val
= get_readable_size(getfilesize
, precision
, unit
);
374 hashtypelist
= usehashtypes
.split(",");
376 listnumend
= len(hashtypelist
);
377 while(listnumcount
< listnumend
):
378 hashtypelistlow
= hashtypelist
[listnumcount
].strip();
379 hashtypelistup
= hashtypelistlow
.upper();
380 filehash
= hashlib
.new(hashtypelistup
);
381 if(sys
.version
[0]=="2"):
382 filehash
.update(instring
);
383 if(sys
.version
[0]>="3"):
384 filehash
.update(instring
.encode('utf-8'));
385 filegethash
= filehash
.hexdigest();
386 return_val
.update({hashtypelistup
: filegethash
});
390 def http_status_to_reason(code
):
393 101: 'Switching Protocols',
398 203: 'Non-Authoritative Information',
400 205: 'Reset Content',
401 206: 'Partial Content',
403 208: 'Already Reported',
405 300: 'Multiple Choices',
406 301: 'Moved Permanently',
411 307: 'Temporary Redirect',
412 308: 'Permanent Redirect',
415 402: 'Payment Required',
418 405: 'Method Not Allowed',
419 406: 'Not Acceptable',
420 407: 'Proxy Authentication Required',
421 408: 'Request Timeout',
424 411: 'Length Required',
425 412: 'Precondition Failed',
426 413: 'Payload Too Large',
428 415: 'Unsupported Media Type',
429 416: 'Range Not Satisfiable',
430 417: 'Expectation Failed',
431 421: 'Misdirected Request',
432 422: 'Unprocessable Entity',
434 424: 'Failed Dependency',
435 426: 'Upgrade Required',
436 428: 'Precondition Required',
437 429: 'Too Many Requests',
438 431: 'Request Header Fields Too Large',
439 451: 'Unavailable For Legal Reasons',
440 500: 'Internal Server Error',
441 501: 'Not Implemented',
443 503: 'Service Unavailable',
444 504: 'Gateway Timeout',
445 505: 'HTTP Version Not Supported',
446 506: 'Variant Also Negotiates',
447 507: 'Insufficient Storage',
448 508: 'Loop Detected',
450 511: 'Network Authentication Required'
452 return reasons
.get(code
, 'Unknown Status Code');
454 def ftp_status_to_reason(code
):
456 110: 'Restart marker reply',
457 120: 'Service ready in nnn minutes',
458 125: 'Data connection already open; transfer starting',
459 150: 'File status okay; about to open data connection',
461 202: 'Command not implemented, superfluous at this site',
462 211: 'System status, or system help reply',
463 212: 'Directory status',
466 215: 'NAME system type',
467 220: 'Service ready for new user',
468 221: 'Service closing control connection',
469 225: 'Data connection open; no transfer in progress',
470 226: 'Closing data connection',
471 227: 'Entering Passive Mode',
472 230: 'User logged in, proceed',
473 250: 'Requested file action okay, completed',
474 257: '"PATHNAME" created',
475 331: 'User name okay, need password',
476 332: 'Need account for login',
477 350: 'Requested file action pending further information',
478 421: 'Service not available, closing control connection',
479 425: 'Can\'t open data connection',
480 426: 'Connection closed; transfer aborted',
481 450: 'Requested file action not taken',
482 451: 'Requested action aborted. Local error in processing',
483 452: 'Requested action not taken. Insufficient storage space in system',
484 500: 'Syntax error, command unrecognized',
485 501: 'Syntax error in parameters or arguments',
486 502: 'Command not implemented',
487 503: 'Bad sequence of commands',
488 504: 'Command not implemented for that parameter',
489 530: 'Not logged in',
490 532: 'Need account for storing files',
491 550: 'Requested action not taken. File unavailable',
492 551: 'Requested action aborted. Page type unknown',
493 552: 'Requested file action aborted. Exceeded storage allocation',
494 553: 'Requested action not taken. File name not allowed'
496 return reasons
.get(code
, 'Unknown Status Code');
498 def sftp_status_to_reason(code
):
502 2: 'SSH_FX_NO_SUCH_FILE',
503 3: 'SSH_FX_PERMISSION_DENIED',
505 5: 'SSH_FX_BAD_MESSAGE',
506 6: 'SSH_FX_NO_CONNECTION',
507 7: 'SSH_FX_CONNECTION_LOST',
508 8: 'SSH_FX_OP_UNSUPPORTED'
510 return reasons
.get(code
, 'Unknown Status Code');
512 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"}):
513 if isinstance(headers
, dict):
515 if(sys
.version
[0]=="2"):
516 for headkey
, headvalue
in headers
.iteritems():
517 returnval
.append((headkey
, headvalue
));
518 if(sys
.version
[0]>="3"):
519 for headkey
, headvalue
in headers
.items():
520 returnval
.append((headkey
, headvalue
));
521 elif isinstance(headers
, list):
527 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"}):
528 if isinstance(headers
, dict):
530 if(sys
.version
[0]=="2"):
531 for headkey
, headvalue
in headers
.iteritems():
532 returnval
.append(headkey
+": "+headvalue
);
533 if(sys
.version
[0]>="3"):
534 for headkey
, headvalue
in headers
.items():
535 returnval
.append(headkey
+": "+headvalue
);
536 elif isinstance(headers
, list):
542 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")]):
543 if isinstance(headers
, list):
548 returnval
.update({headers
[mli
][0]: headers
[mli
][1]});
550 elif isinstance(headers
, dict):
556 def get_httplib_support(checkvalue
=None):
557 global haverequests
, havemechanize
, havehttplib2
, haveurllib3
, havehttpx
, havehttpcore
, haveparamiko
, havepysftp
;
559 returnval
.append("ftp");
560 returnval
.append("httplib");
562 returnval
.append("httplib2");
563 returnval
.append("urllib");
565 returnval
.append("urllib3");
566 returnval
.append("request3");
567 returnval
.append("request");
569 returnval
.append("requests");
571 returnval
.append("httpx");
572 returnval
.append("httpx2");
574 returnval
.append("mechanize");
576 returnval
.append("sftp");
578 returnval
.append("pysftp");
579 if(not checkvalue
is None):
580 if(checkvalue
=="urllib1" or checkvalue
=="urllib2"):
581 checkvalue
= "urllib";
582 if(checkvalue
=="httplib1"):
583 checkvalue
= "httplib";
584 if(checkvalue
in returnval
):
590 def check_httplib_support(checkvalue
="urllib"):
591 if(checkvalue
=="urllib1" or checkvalue
=="urllib2"):
592 checkvalue
= "urllib";
593 if(checkvalue
=="httplib1"):
594 checkvalue
= "httplib";
595 returnval
= get_httplib_support(checkvalue
);
598 def get_httplib_support_list():
599 returnval
= get_httplib_support(None);
602 def download_from_url(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, httplibuse
="urllib", sleep
=-1):
603 global geturls_download_sleep
, haverequests
, havemechanize
, havehttplib2
, haveurllib3
, havehttpx
, havehttpcore
, haveparamiko
, havepysftp
;
605 sleep
= geturls_download_sleep
;
606 if(httplibuse
=="urllib1" or httplibuse
=="urllib2"):
607 httplibuse
= "urllib";
608 if(httplibuse
=="httplib1"):
609 httplibuse
= "httplib";
610 if(not haverequests
and httplibuse
=="requests"):
611 httplibuse
= "urllib";
612 if(not havehttpx
and httplibuse
=="httpx"):
613 httplibuse
= "urllib";
614 if(not havehttpx
and httplibuse
=="httpx2"):
615 httplibuse
= "urllib";
616 if(not havehttpcore
and httplibuse
=="httpcore"):
617 httplibuse
= "urllib";
618 if(not havehttpcore
and httplibuse
=="httpcore2"):
619 httplibuse
= "urllib";
620 if(not havemechanize
and httplibuse
=="mechanize"):
621 httplibuse
= "urllib";
622 if(not havehttplib2
and httplibuse
=="httplib2"):
623 httplibuse
= "httplib";
624 if(not haveparamiko
and httplibuse
=="sftp"):
626 if(not havepysftp
and httplibuse
=="pysftp"):
628 urlparts
= urlparse
.urlparse(httpurl
);
629 if(isinstance(httpheaders
, list)):
630 httpheaders
= make_http_headers_from_list_to_dict(httpheaders
);
631 httpheaders
= fix_header_names(httpheaders
);
632 if(httpuseragent
is not None):
633 if('User-Agent' in httpheaders
):
634 httpheaders
['User-Agent'] = httpuseragent
;
636 httpuseragent
.update({'User-Agent': httpuseragent
});
637 if(httpreferer
is not None):
638 if('Referer' in httpheaders
):
639 httpheaders
['Referer'] = httpreferer
;
641 httpuseragent
.update({'Referer': httpreferer
});
642 if(urlparts
.username
is not None or urlparts
.password
is not None):
643 inurlencode
= b64encode(str(urlparts
.username
+":"+urlparts
.password
).encode()).decode("UTF-8");
644 httpheaders
.update( { 'Authorization': "Basic "+inurlencode
} );
645 geturls_opener
= build_opener(HTTPCookieProcessor(httpcookie
));
646 if(httplibuse
=="urllib" or httplibuse
=="request" or httplibuse
=="mechanize"):
647 if(isinstance(httpheaders
, dict)):
648 httpheaders
= make_http_headers_from_dict_to_list(httpheaders
);
649 geturls_opener
.addheaders
= httpheaders
;
651 if(postdata
is not None and not isinstance(postdata
, dict)):
652 postdata
= urlencode(postdata
);
653 if(httplibuse
=="urllib"):
655 if(httpmethod
=="GET"):
656 geturls_text
= geturls_opener
.open(httpurl
);
657 elif(httpmethod
=="POST"):
658 geturls_text
= geturls_opener
.open(httpurl
, data
=postdata
);
660 geturls_text
= geturls_opener
.open(httpurl
);
661 except HTTPError
as geturls_text_error
:
662 geturls_text
= geturls_text_error
;
663 log
.info("Error With URL "+httpurl
);
665 log
.info("Error With URL "+httpurl
);
667 except socket
.timeout
:
668 log
.info("Error With URL "+httpurl
);
670 httpcodeout
= geturls_text
.getcode();
671 httpcodereason
= geturls_text
.reason
;
672 httpversionout
= "1.1";
673 httpmethodout
= httpmethod
;
674 httpurlout
= geturls_text
.geturl();
675 httpheaderout
= geturls_text
.info();
676 httpheadersentout
= httpheaders
;
677 elif(httplibuse
=="request"):
679 if(httpmethod
=="GET"):
680 geturls_request
= Request(httpurl
, headers
=httpheaders
);
681 geturls_text
= urlopen(geturls_request
);
682 elif(httpmethod
=="POST"):
683 geturls_request
= Request(httpurl
, headers
=httpheaders
);
684 geturls_text
= urlopen(geturls_request
, data
=postdata
);
686 geturls_request
= Request(httpurl
, headers
=httpheaders
);
687 geturls_text
= urlopen(geturls_request
);
688 except HTTPError
as geturls_text_error
:
689 geturls_text
= geturls_text_error
;
690 log
.info("Error With URL "+httpurl
);
692 log
.info("Error With URL "+httpurl
);
694 except socket
.timeout
:
695 log
.info("Error With URL "+httpurl
);
697 httpcodeout
= geturls_text
.getcode();
698 httpcodereason
= geturls_text
.reason
;
699 httpversionout
= "1.1";
700 httpmethodout
= httpmethod
;
701 httpurlout
= geturls_text
.geturl();
702 httpheaderout
= geturls_text
.headers
;
703 httpheadersentout
= httpheaders
;
704 elif(httplibuse
=="request3"):
705 urllib_pool
= urllib3
.PoolManager(headers
=httpheaders
);
707 if(httpmethod
=="GET"):
708 geturls_text
= geturls_text
= urllib_pool
.request("GET", httpurl
, headers
=httpheaders
, preload_content
=False);
709 elif(httpmethod
=="POST"):
710 geturls_text
= geturls_text
= urllib_pool
.request("POST", httpurl
, body
=postdata
, headers
=httpheaders
, preload_content
=False);
712 geturls_text
= geturls_text
= urllib_pool
.request("GET", httpurl
, headers
=httpheaders
, preload_content
=False);
713 except urllib3
.exceptions
.ConnectTimeoutError
:
714 log
.info("Error With URL "+httpurl
);
716 except urllib3
.exceptions
.ConnectError
:
717 log
.info("Error With URL "+httpurl
);
719 except urllib3
.exceptions
.MaxRetryError
:
720 log
.info("Error With URL "+httpurl
);
722 except socket
.timeout
:
723 log
.info("Error With URL "+httpurl
);
725 httpcodeout
= geturls_text
.status
;
726 httpcodereason
= geturls_text
.reason
;
727 if(geturls_text
.version
=="10"):
728 httpversionout
= "1.0";
730 httpversionout
= "1.1";
731 httpmethodout
= httpmethod
;
732 httpurlout
= geturls_text
.geturl();
733 httpheaderout
= geturls_text
.info();
734 httpheadersentout
= httpheaders
;
735 elif(httplibuse
=="httplib"):
736 if(urlparts
[0]=="http"):
737 httpconn
= HTTPConnection(urlparts
[1]);
738 elif(urlparts
[0]=="https"):
739 httpconn
= HTTPSConnection(urlparts
[1]);
742 if(postdata
is not None and not isinstance(postdata
, dict)):
743 postdata
= urlencode(postdata
);
745 if(httpmethod
=="GET"):
746 httpconn
.request("GET", urlparts
[2], headers
=httpheaders
);
747 elif(httpmethod
=="POST"):
748 httpconn
.request("GET", urlparts
[2], body
=postdata
, headers
=httpheaders
);
750 httpconn
.request("GET", urlparts
[2], headers
=httpheaders
);
751 except socket
.timeout
:
752 log
.info("Error With URL "+httpurl
);
754 except socket
.gaierror
:
755 log
.info("Error With URL "+httpurl
);
757 geturls_text
= httpconn
.getresponse();
758 httpcodeout
= geturls_text
.status
;
759 httpcodereason
= geturls_text
.reason
;
760 if(geturls_text
.version
=="10"):
761 httpversionout
= "1.0";
763 httpversionout
= "1.1";
764 httpmethodout
= httpmethod
;
765 httpurlout
= httpurl
;
766 httpheaderout
= geturls_text
.getheaders();
767 httpheadersentout
= httpheaders
;
768 elif(httplibuse
=="httplib2"):
769 if(urlparts
[0]=="http"):
770 httpconn
= HTTPConnectionWithTimeout(urlparts
[1]);
771 elif(urlparts
[0]=="https"):
772 httpconn
= HTTPSConnectionWithTimeout(urlparts
[1]);
775 if(postdata
is not None and not isinstance(postdata
, dict)):
776 postdata
= urlencode(postdata
);
778 if(httpmethod
=="GET"):
779 httpconn
.request("GET", urlparts
[2], headers
=httpheaders
);
780 elif(httpmethod
=="POST"):
781 httpconn
.request("GET", urlparts
[2], body
=postdata
, headers
=httpheaders
);
783 httpconn
.request("GET", urlparts
[2], headers
=httpheaders
);
784 except socket
.timeout
:
785 log
.info("Error With URL "+httpurl
);
787 except socket
.gaierror
:
788 log
.info("Error With URL "+httpurl
);
790 geturls_text
= httpconn
.getresponse();
791 httpcodeout
= geturls_text
.status
;
792 httpcodereason
= geturls_text
.reason
;
793 if(geturls_text
.version
=="10"):
794 httpversionout
= "1.0";
796 httpversionout
= "1.1";
797 httpmethodout
= httpmethod
;
798 httpurlout
= httpurl
;
799 httpheaderout
= geturls_text
.getheaders();
800 httpheadersentout
= httpheaders
;
801 elif(httplibuse
=="urllib3"):
802 urllib_pool
= urllib3
.PoolManager(headers
=httpheaders
);
804 if(httpmethod
=="GET"):
805 geturls_text
= urllib_pool
.urlopen("GET", httpurl
, headers
=httpheaders
, preload_content
=False);
806 elif(httpmethod
=="POST"):
807 geturls_text
= urllib_pool
.urlopen("GET", httpurl
, body
=postdata
, headers
=httpheaders
, preload_content
=False);
809 geturls_text
= urllib_pool
.urlopen("GET", httpurl
, headers
=httpheaders
, preload_content
=False);
810 except urllib3
.exceptions
.ConnectTimeoutError
:
811 log
.info("Error With URL "+httpurl
);
813 except urllib3
.exceptions
.ConnectError
:
814 log
.info("Error With URL "+httpurl
);
816 except urllib3
.exceptions
.MaxRetryError
:
817 log
.info("Error With URL "+httpurl
);
819 except socket
.timeout
:
820 log
.info("Error With URL "+httpurl
);
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
= geturls_text
.geturl();
830 httpheaderout
= geturls_text
.info();
831 httpheadersentout
= httpheaders
;
832 elif(httplibuse
=="requests"):
834 reqsession
= requests
.Session();
835 if(httpmethod
=="GET"):
836 geturls_text
= reqsession
.get(httpurl
, headers
=httpheaders
, cookies
=httpcookie
);
837 elif(httpmethod
=="POST"):
838 geturls_text
= reqsession
.post(httpurl
, data
=postdata
, headers
=httpheaders
, cookies
=httpcookie
);
840 geturls_text
= reqsession
.get(httpurl
, headers
=httpheaders
, cookies
=httpcookie
);
841 except requests
.exceptions
.ConnectTimeout
:
842 log
.info("Error With URL "+httpurl
);
844 except requests
.exceptions
.ConnectError
:
845 log
.info("Error With URL "+httpurl
);
847 except socket
.timeout
:
848 log
.info("Error With URL "+httpurl
);
850 httpcodeout
= geturls_text
.status_code
;
851 httpcodereason
= geturls_text
.reason
;
852 if(geturls_text
.raw
.version
=="10"):
853 httpversionout
= "1.0";
855 httpversionout
= "1.1";
856 httpmethodout
= httpmethod
;
857 httpurlout
= geturls_text
.url
;
858 httpheaderout
= geturls_text
.headers
;
859 httpheadersentout
= geturls_text
.request
.headers
;
860 elif(httplibuse
=="httpx"):
862 if(httpmethod
=="GET"):
863 httpx_pool
= httpx
.Client(http1
=True, http2
=False, trust_env
=True);
864 geturls_text
= httpx_pool
.get(httpurl
, headers
=httpheaders
, cookies
=httpcookie
);
865 elif(httpmethod
=="POST"):
866 httpx_pool
= httpx
.Client(http1
=True, http2
=False, trust_env
=True);
867 geturls_text
= httpx_pool
.post(httpurl
, data
=postdata
, headers
=httpheaders
, cookies
=httpcookie
);
869 httpx_pool
= httpx
.Client(http1
=True, http2
=False, trust_env
=True);
870 geturls_text
= httpx_pool
.get(httpurl
, headers
=httpheaders
, cookies
=httpcookie
);
871 except httpx
.ConnectTimeout
:
872 log
.info("Error With URL "+httpurl
);
874 except httpx
.ConnectError
:
875 log
.info("Error With URL "+httpurl
);
877 except socket
.timeout
:
878 log
.info("Error With URL "+httpurl
);
880 httpcodeout
= geturls_text
.status_code
;
881 httpcodereason
= geturls_text
.reason_phrase
;
882 httpversionout
= geturls_text
.http_version
;
883 httpmethodout
= httpmethod
;
884 httpurlout
= str(geturls_text
.url
);
885 httpheaderout
= geturls_text
.headers
;
886 httpheadersentout
= geturls_text
.request
.headers
;
887 elif(httplibuse
=="httpx2"):
889 if(httpmethod
=="GET"):
890 httpx_pool
= httpx
.Client(http1
=True, http2
=True, trust_env
=True);
891 geturls_text
= httpx_pool
.get(httpurl
, headers
=httpheaders
, cookies
=httpcookie
);
892 elif(httpmethod
=="POST"):
893 httpx_pool
= httpx
.Client(http1
=True, http2
=True, trust_env
=True);
894 geturls_text
= httpx_pool
.post(httpurl
, data
=postdata
, headers
=httpheaders
, cookies
=httpcookie
);
896 httpx_pool
= httpx
.Client(http1
=True, http2
=True, trust_env
=True);
897 geturls_text
= httpx_pool
.get(httpurl
, headers
=httpheaders
, cookies
=httpcookie
);
898 except httpx
.ConnectTimeout
:
899 log
.info("Error With URL "+httpurl
);
901 except httpx
.ConnectError
:
902 log
.info("Error With URL "+httpurl
);
904 except socket
.timeout
:
905 log
.info("Error With URL "+httpurl
);
907 httpcodeout
= geturls_text
.status_code
;
908 httpcodereason
= geturls_text
.reason
;
909 httpversionout
= geturls_text
.http_version
;
910 httpmethodout
= httpmethod
;
911 httpurlout
= str(geturls_text
.url
);
912 httpheaderout
= geturls_text
.headers
;
913 httpheadersentout
= geturls_text
.request
.headers
;
914 elif(httplibuse
=="httpcore"):
916 if(httpmethod
=="GET"):
917 httpx_pool
= httpcore
.ConnectionPool(http1
=True, http2
=False);
918 geturls_text
= httpx_pool
.request("GET", httpurl
, headers
=httpheaders
);
919 elif(httpmethod
=="POST"):
920 httpx_pool
= httpcore
.ConnectionPool(http1
=True, http2
=False);
921 geturls_text
= httpx_pool
.request("GET", httpurl
, data
=postdata
, headers
=httpheaders
);
923 httpx_pool
= httpcore
.ConnectionPool(http1
=True, http2
=False);
924 geturls_text
= httpx_pool
.request("GET", httpurl
, headers
=httpheaders
);
925 except httpcore
.ConnectTimeout
:
926 log
.info("Error With URL "+httpurl
);
928 except httpcore
.ConnectError
:
929 log
.info("Error With URL "+httpurl
);
931 except socket
.timeout
:
932 log
.info("Error With URL "+httpurl
);
934 httpcodeout
= geturls_text
.status
;
935 httpcodereason
= http_status_to_reason(geturls_text
.status
);
936 httpversionout
= "1.1";
937 httpmethodout
= httpmethod
;
938 httpurlout
= str(httpurl
);
939 httpheaderout
= geturls_text
.headers
;
940 httpheadersentout
= httpheaders
;
941 elif(httplibuse
=="httpcore2"):
943 if(httpmethod
=="GET"):
944 httpx_pool
= httpcore
.ConnectionPool(http1
=True, http2
=True);
945 geturls_text
= httpx_pool
.request("GET", httpurl
, headers
=httpheaders
);
946 elif(httpmethod
=="POST"):
947 httpx_pool
= httpcore
.ConnectionPool(http1
=True, http2
=True);
948 geturls_text
= httpx_pool
.request("GET", httpurl
, data
=postdata
, headers
=httpheaders
);
950 httpx_pool
= httpcore
.ConnectionPool(http1
=True, http2
=True);
951 geturls_text
= httpx_pool
.request("GET", httpurl
, headers
=httpheaders
);
952 except httpcore
.ConnectTimeout
:
953 log
.info("Error With URL "+httpurl
);
955 except httpcore
.ConnectError
:
956 log
.info("Error With URL "+httpurl
);
958 except socket
.timeout
:
959 log
.info("Error With URL "+httpurl
);
961 httpcodeout
= geturls_text
.status
;
962 httpcodereason
= geturls_text
.reason
;
963 httpversionout
= "1.1";
964 httpmethodout
= httpmethod
;
965 httpurlout
= str(httpurl
);
966 httpheaderout
= geturls_text
.headers
;
967 httpheadersentout
= httpheaders
;
968 elif(httplibuse
=="mechanize"):
969 geturls_opener
= mechanize
.Browser();
970 if(isinstance(httpheaders
, dict)):
971 httpheaders
= make_http_headers_from_dict_to_list(httpheaders
);
973 geturls_opener
.addheaders
= httpheaders
;
974 geturls_opener
.set_cookiejar(httpcookie
);
975 geturls_opener
.set_handle_robots(False);
976 if(postdata
is not None and not isinstance(postdata
, dict)):
977 postdata
= urlencode(postdata
);
979 if(httpmethod
=="GET"):
980 geturls_text
= geturls_opener
.open(httpurl
);
981 elif(httpmethod
=="POST"):
982 geturls_text
= geturls_opener
.open(httpurl
, data
=postdata
);
984 geturls_text
= geturls_opener
.open(httpurl
);
985 except mechanize
.HTTPError
as geturls_text_error
:
986 geturls_text
= geturls_text_error
;
987 log
.info("Error With URL "+httpurl
);
989 log
.info("Error With URL "+httpurl
);
991 except socket
.timeout
:
992 log
.info("Error With URL "+httpurl
);
994 httpcodeout
= geturls_text
.code
;
995 httpcodereason
= http_status_to_reason(geturls_text
.code
);
996 httpversionout
= "1.1";
997 httpmethodout
= httpmethod
;
998 httpurlout
= geturls_text
.geturl();
999 httpheaderout
= geturls_text
.info();
1000 reqhead
= geturls_opener
.request
;
1001 httpheadersentout
= reqhead
.header_items();
1003 elif(httplibuse
=="ftp"):
1004 geturls_text
= download_file_from_ftp_file(httpurl
);
1005 if(not geturls_text
):
1007 log
.info("Downloading URL "+httpurl
);
1008 returnval_content
= geturls_text
.read()[:];
1009 returnval
= {'Type': "Content", 'Content': returnval_content
, 'Headers': None, 'Version': None, 'Method': None, 'HeadersSent': None, 'URL': httpurl
, 'Code': None};
1010 geturls_text
.close();
1011 elif(httplibuse
=="sftp"):
1012 geturls_text
= download_file_from_sftp_file(httpurl
);
1013 if(not geturls_text
):
1015 log
.info("Downloading URL "+httpurl
);
1016 returnval_content
= geturls_text
.read()[:];
1017 returnval
= {'Type': "Content", 'Content': returnval_content
, 'Headers': None, 'Version': None, 'Method': None, 'HeadersSent': None, 'URL': httpurl
, 'Code': None};
1018 geturls_text
.close();
1020 elif(httplibuse
=="pysftp"):
1021 geturls_text
= download_file_from_pysftp_file(httpurl
);
1022 if(not geturls_text
):
1024 log
.info("Downloading URL "+httpurl
);
1025 returnval_content
= geturls_text
.read()[:];
1026 returnval
= {'Type': "Content", 'Content': returnval_content
, 'Headers': None, 'Version': None, 'Method': None, 'HeadersSent': None, 'URL': httpurl
, 'Code': None};
1027 geturls_text
.close();
1031 if(isinstance(httpheaderout
, list)):
1032 httpheaderout
= dict(make_http_headers_from_list_to_dict(httpheaderout
));
1033 if(sys
.version
[0]=="2"):
1035 prehttpheaderout
= httpheaderout
;
1036 httpheaderkeys
= httpheaderout
.keys();
1037 imax
= len(httpheaderkeys
);
1041 httpheaderout
.update({httpheaderkeys
[ic
]: prehttpheaderout
[httpheaderkeys
[ic
]]});
1043 except AttributeError:
1045 httpheaderout
= fix_header_names(httpheaderout
);
1046 if(isinstance(httpheadersentout
, list)):
1047 httpheadersentout
= dict(make_http_headers_from_list_to_dict(httpheadersentout
));
1048 httpheadersentout
= fix_header_names(httpheadersentout
);
1049 log
.info("Downloading URL "+httpurl
);
1050 if(httplibuse
=="urllib" or httplibuse
=="request" or httplibuse
=="request3" or httplibuse
=="httplib" or httplibuse
=="httplib2" or httplibuse
=="urllib3" or httplibuse
=="mechanize" or httplibuse
=="httpx" or httplibuse
=="httpx2" or httplibuse
=="httpcore" or httplibuse
=="httpcore2"):
1051 if(httpheaderout
.get("Content-Encoding")=="gzip" or httpheaderout
.get("Content-Encoding")=="deflate"):
1052 if(sys
.version
[0]=="2"):
1053 strbuf
= StringIO(geturls_text
.read());
1054 if(sys
.version
[0]>="3"):
1055 strbuf
= BytesIO(geturls_text
.read());
1056 gzstrbuf
= gzip
.GzipFile(fileobj
=strbuf
);
1057 returnval_content
= gzstrbuf
.read()[:];
1058 if(httpheaderout
.get("Content-Encoding")!="gzip" and httpheaderout
.get("Content-Encoding")!="deflate" and httpheaderout
.get("Content-Encoding")!="br"):
1059 returnval_content
= geturls_text
.read()[:];
1060 if(httpheaderout
.get("Content-Encoding")=="br" and havebrotli
):
1061 returnval_content
= geturls_text
.read()[:];
1062 returnval_content
= brotli
.decompress(returnval_content
);
1063 geturls_text
.close();
1064 elif(httplibuse
=="requests"):
1065 log
.info("Downloading URL "+httpurl
);
1066 if(httpheaderout
.get("Content-Encoding")=="gzip" or httpheaderout
.get("Content-Encoding")=="deflate"):
1067 if(sys
.version
[0]=="2"):
1068 strbuf
= StringIO(geturls_text
.raw
.read());
1069 if(sys
.version
[0]>="3"):
1070 strbuf
= BytesIO(geturls_text
.raw
.read());
1071 gzstrbuf
= gzip
.GzipFile(fileobj
=strbuf
);
1072 returnval_content
= gzstrbuf
.read()[:];
1073 if(httpheaderout
.get("Content-Encoding")!="gzip" and httpheaderout
.get("Content-Encoding")!="deflate" and httpheaderout
.get("Content-Encoding")!="br"):
1074 returnval_content
= geturls_text
.raw
.read()[:];
1075 if(httpheaderout
.get("Content-Encoding")=="br" and havebrotli
):
1076 returnval_content
= geturls_text
.raw
.read()[:];
1077 returnval_content
= brotli
.decompress(returnval_content
);
1078 geturls_text
.close();
1079 elif(httplibuse
=="ftp" or httplibuse
=="sftp" or httplibuse
=="pysftp"):
1083 returnval
= {'Type': "Content", 'Content': returnval_content
, 'Headers': httpheaderout
, 'Version': httpversionout
, 'Method': httpmethodout
, 'HeadersSent': httpheadersentout
, 'URL': httpurlout
, 'Code': httpcodeout
, 'Reason': httpcodereason
};
1086 def download_from_url_file(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, httplibuse
="urllib", buffersize
=524288, sleep
=-1):
1087 global geturls_download_sleep
, tmpfileprefix
, tmpfilesuffix
, haverequests
, havemechanize
, havehttplib2
, haveurllib3
, havehttpx
, havehttpcore
, haveparamiko
, havepysftp
;
1088 exec_time_start
= time
.time();
1089 myhash
= hashlib
.new("sha1");
1090 if(sys
.version
[0]=="2"):
1091 myhash
.update(httpurl
);
1092 myhash
.update(str(buffersize
));
1093 myhash
.update(str(exec_time_start
));
1094 if(sys
.version
[0]>="3"):
1095 myhash
.update(httpurl
.encode('utf-8'));
1096 myhash
.update(str(buffersize
).encode('utf-8'));
1097 myhash
.update(str(exec_time_start
).encode('utf-8'));
1098 newtmpfilesuffix
= tmpfilesuffix
+ str(myhash
.hexdigest());
1100 sleep
= geturls_download_sleep
;
1101 if(httplibuse
=="urllib1" or httplibuse
=="urllib2"):
1102 httplibuse
= "urllib";
1103 if(httplibuse
=="httplib1"):
1104 httplibuse
= "httplib";
1105 if(not haverequests
and httplibuse
=="requests"):
1106 httplibuse
= "urllib";
1107 if(not havehttpx
and httplibuse
=="httpx"):
1108 httplibuse
= "urllib";
1109 if(not havehttpx
and httplibuse
=="httpx2"):
1110 httplibuse
= "urllib";
1111 if(not havehttpcore
and httplibuse
=="httpcore"):
1112 httplibuse
= "urllib";
1113 if(not havehttpcore
and httplibuse
=="httpcore2"):
1114 httplibuse
= "urllib";
1115 if(not havemechanize
and httplibuse
=="mechanize"):
1116 httplibuse
= "urllib";
1117 if(not havehttplib2
and httplibuse
=="httplib2"):
1118 httplibuse
= "httplib";
1119 if(not haveparamiko
and httplibuse
=="sftp"):
1121 if(not haveparamiko
and httplibuse
=="pysftp"):
1123 urlparts
= urlparse
.urlparse(httpurl
);
1124 if(isinstance(httpheaders
, list)):
1125 httpheaders
= make_http_headers_from_list_to_dict(httpheaders
);
1126 httpheaders
= fix_header_names(httpheaders
);
1127 if(httpuseragent
is not None):
1128 if('User-Agent' in httpheaders
):
1129 httpheaders
['User-Agent'] = httpuseragent
;
1131 httpuseragent
.update({'User-Agent': httpuseragent
});
1132 if(httpreferer
is not None):
1133 if('Referer' in httpheaders
):
1134 httpheaders
['Referer'] = httpreferer
;
1136 httpuseragent
.update({'Referer': httpreferer
});
1137 if(urlparts
.username
is not None or urlparts
.password
is not None):
1138 inurlencode
= b64encode(str(urlparts
.username
+":"+urlparts
.password
).encode()).decode("UTF-8");
1139 httpheaders
.update( { 'Authorization': "Basic "+inurlencode
} );
1140 geturls_opener
= build_opener(HTTPCookieProcessor(httpcookie
));
1141 if(httplibuse
=="urllib" or httplibuse
=="request" or httplibuse
=="mechanize"):
1142 if(isinstance(httpheaders
, dict)):
1143 httpheaders
= make_http_headers_from_dict_to_list(httpheaders
);
1144 geturls_opener
.addheaders
= httpheaders
;
1146 if(httplibuse
=="urllib"):
1148 if(httpmethod
=="GET"):
1149 geturls_text
= geturls_opener
.open(httpurl
);
1150 elif(httpmethod
=="POST"):
1151 geturls_text
= geturls_opener
.open(httpurl
, data
=postdata
);
1153 geturls_text
= geturls_opener
.open(httpurl
);
1154 except HTTPError
as geturls_text_error
:
1155 geturls_text
= geturls_text_error
;
1156 log
.info("Error With URL "+httpurl
);
1158 log
.info("Error With URL "+httpurl
);
1160 except socket
.timeout
:
1161 log
.info("Error With URL "+httpurl
);
1163 except socket
.timeout
:
1164 log
.info("Error With URL "+httpurl
);
1166 httpcodeout
= geturls_text
.getcode();
1167 httpcodereason
= geturls_text
.reason
;
1168 httpversionout
= "1.1";
1169 httpmethodout
= httpmethod
;
1170 httpurlout
= geturls_text
.geturl();
1171 httpheaderout
= geturls_text
.info();
1172 httpheadersentout
= httpheaders
;
1173 elif(httplibuse
=="request"):
1175 if(httpmethod
=="GET"):
1176 geturls_request
= Request(httpurl
, headers
=httpheaders
);
1177 geturls_text
= urlopen(geturls_request
);
1178 elif(httpmethod
=="POST"):
1179 geturls_request
= Request(httpurl
, headers
=httpheaders
);
1180 geturls_text
= urlopen(geturls_request
, data
=postdata
);
1182 geturls_request
= Request(httpurl
, headers
=httpheaders
);
1183 geturls_text
= urlopen(geturls_request
);
1184 except HTTPError
as geturls_text_error
:
1185 geturls_text
= geturls_text_error
;
1186 log
.info("Error With URL "+httpurl
);
1188 log
.info("Error With URL "+httpurl
);
1190 except socket
.timeout
:
1191 log
.info("Error With URL "+httpurl
);
1193 httpcodeout
= geturls_text
.getcode();
1194 httpcodereason
= geturls_text
.reason
;
1195 httpversionout
= "1.1";
1196 httpmethodout
= httpmethod
;
1197 httpurlout
= geturls_text
.geturl();
1198 httpheaderout
= geturls_text
.headers
;
1199 httpheadersentout
= httpheaders
;
1200 elif(httplibuse
=="request3"):
1201 urllib_pool
= urllib3
.PoolManager(headers
=httpheaders
);
1203 if(httpmethod
=="GET"):
1204 geturls_text
= geturls_text
= urllib_pool
.request("GET", httpurl
, headers
=httpheaders
, preload_content
=False);
1205 elif(httpmethod
=="POST"):
1206 geturls_text
= geturls_text
= urllib_pool
.request("POST", httpurl
, body
=postdata
, headers
=httpheaders
, preload_content
=False);
1208 geturls_text
= geturls_text
= urllib_pool
.request("GET", httpurl
, headers
=httpheaders
, preload_content
=False);
1209 except urllib3
.exceptions
.ConnectTimeoutError
:
1210 log
.info("Error With URL "+httpurl
);
1212 except urllib3
.exceptions
.ConnectError
:
1213 log
.info("Error With URL "+httpurl
);
1215 except urllib3
.exceptions
.MaxRetryError
:
1216 log
.info("Error With URL "+httpurl
);
1218 except socket
.timeout
:
1219 log
.info("Error With URL "+httpurl
);
1221 httpcodeout
= geturls_text
.status
;
1222 httpcodereason
= geturls_text
.reason
;
1223 if(geturls_text
.version
=="10"):
1224 httpversionout
= "1.0";
1226 httpversionout
= "1.1";
1227 httpmethodout
= httpmethod
;
1228 httpurlout
= geturls_text
.geturl();
1229 httpheaderout
= geturls_text
.info();
1230 httpheadersentout
= httpheaders
;
1231 elif(httplibuse
=="httplib"):
1232 if(urlparts
[0]=="http"):
1233 httpconn
= HTTPConnection(urlparts
[1]);
1234 elif(urlparts
[0]=="https"):
1235 httpconn
= HTTPSConnection(urlparts
[1]);
1238 if(postdata
is not None and not isinstance(postdata
, dict)):
1239 postdata
= urlencode(postdata
);
1241 if(httpmethod
=="GET"):
1242 httpconn
.request("GET", urlparts
[2], headers
=httpheaders
);
1243 elif(httpmethod
=="POST"):
1244 httpconn
.request("GET", urlparts
[2], body
=postdata
, headers
=httpheaders
);
1246 httpconn
.request("GET", urlparts
[2], headers
=httpheaders
);
1247 except socket
.timeout
:
1248 log
.info("Error With URL "+httpurl
);
1250 except socket
.gaierror
:
1251 log
.info("Error With URL "+httpurl
);
1253 geturls_text
= httpconn
.getresponse();
1254 httpcodeout
= geturls_text
.status
;
1255 httpcodereason
= geturls_text
.reason
;
1256 if(geturls_text
.version
=="10"):
1257 httpversionout
= "1.0";
1259 httpversionout
= "1.1";
1260 httpmethodout
= httpmethod
;
1261 httpurlout
= httpurl
;
1262 httpheaderout
= geturls_text
.getheaders();
1263 httpheadersentout
= httpheaders
;
1264 elif(httplibuse
=="httplib2"):
1266 if(httpmethod
=="GET"):
1267 httpconn
.request("GET", urlparts
[2], headers
=httpheaders
);
1268 elif(httpmethod
=="POST"):
1269 httpconn
.request("GET", urlparts
[2], body
=postdata
, headers
=httpheaders
);
1271 httpconn
.request("GET", urlparts
[2], headers
=httpheaders
);
1272 except socket
.timeout
:
1273 log
.info("Error With URL "+httpurl
);
1275 except socket
.gaierror
:
1276 log
.info("Error With URL "+httpurl
);
1278 geturls_text
= httpconn
.getresponse();
1279 httpcodeout
= geturls_text
.status
;
1280 httpcodereason
= geturls_text
.reason
;
1281 if(geturls_text
.version
=="10"):
1282 httpversionout
= "1.0";
1284 httpversionout
= "1.1";
1285 httpmethodout
= httpmethod
;
1286 httpurlout
= httpurl
;
1287 httpheaderout
= geturls_text
.getheaders();
1288 httpheadersentout
= httpheaders
;
1289 elif(httplibuse
=="urllib3"):
1290 urllib_pool
= urllib3
.PoolManager(headers
=httpheaders
);
1292 if(httpmethod
=="GET"):
1293 geturls_text
= urllib_pool
.urlopen("GET", httpurl
, headers
=httpheaders
, preload_content
=False);
1294 elif(httpmethod
=="POST"):
1295 geturls_text
= urllib_pool
.urlopen("GET", httpurl
, body
=postdata
, headers
=httpheaders
, preload_content
=False);
1297 geturls_text
= urllib_pool
.urlopen("GET", httpurl
, headers
=httpheaders
, preload_content
=False);
1298 except urllib3
.exceptions
.ConnectTimeoutError
:
1299 log
.info("Error With URL "+httpurl
);
1301 except urllib3
.exceptions
.ConnectError
:
1302 log
.info("Error With URL "+httpurl
);
1304 except urllib3
.exceptions
.MaxRetryError
:
1305 log
.info("Error With URL "+httpurl
);
1307 except socket
.timeout
:
1308 log
.info("Error With URL "+httpurl
);
1310 httpcodeout
= geturls_text
.status
;
1311 httpcodereason
= geturls_text
.reason
;
1312 if(geturls_text
.version
=="10"):
1313 httpversionout
= "1.0";
1315 httpversionout
= "1.1";
1316 httpmethodout
= httpmethod
;
1317 httpurlout
= geturls_text
.geturl();
1318 httpheaderout
= geturls_text
.info();
1319 httpheadersentout
= httpheaders
;
1320 elif(httplibuse
=="requests"):
1322 reqsession
= requests
.Session();
1323 if(httpmethod
=="GET"):
1324 geturls_text
= reqsession
.get(httpurl
, headers
=httpheaders
, cookies
=httpcookie
, stream
=True);
1325 elif(httpmethod
=="POST"):
1326 geturls_text
= reqsession
.post(httpurl
, data
=postdata
, headers
=httpheaders
, cookies
=httpcookie
, stream
=True);
1328 geturls_text
= reqsession
.get(httpurl
, headers
=httpheaders
, cookies
=httpcookie
, stream
=True);
1329 except requests
.exceptions
.ConnectTimeout
:
1330 log
.info("Error With URL "+httpurl
);
1332 except requests
.exceptions
.ConnectError
:
1333 log
.info("Error With URL "+httpurl
);
1335 except socket
.timeout
:
1336 log
.info("Error With URL "+httpurl
);
1338 httpcodeout
= geturls_text
.status_code
;
1339 httpcodereason
= geturls_text
.reason
;
1340 if(geturls_text
.raw
.version
=="10"):
1341 httpversionout
= "1.0";
1343 httpversionout
= "1.1";
1344 httpmethodout
= httpmethod
;
1345 httpurlout
= geturls_text
.url
;
1346 httpheaderout
= geturls_text
.headers
;
1347 httpheadersentout
= geturls_text
.request
.headers
;
1348 elif(httplibuse
=="httpx"):
1350 if(httpmethod
=="GET"):
1351 httpx_pool
= httpx
.Client(http1
=True, http2
=False, trust_env
=True);
1352 geturls_text
= httpx_pool
.get(httpurl
, headers
=httpheaders
, cookies
=httpcookie
);
1353 elif(httpmethod
=="POST"):
1354 httpx_pool
= httpx
.Client(http1
=True, http2
=False, trust_env
=True);
1355 geturls_text
= httpx_pool
.post(httpurl
, data
=postdata
, headers
=httpheaders
, cookies
=httpcookie
);
1357 httpx_pool
= httpx
.Client(http1
=True, http2
=False, trust_env
=True);
1358 geturls_text
= httpx_pool
.get(httpurl
, headers
=httpheaders
, cookies
=httpcookie
);
1359 except httpx
.ConnectTimeout
:
1360 log
.info("Error With URL "+httpurl
);
1362 except httpx
.ConnectError
:
1363 log
.info("Error With URL "+httpurl
);
1365 except socket
.timeout
:
1366 log
.info("Error With URL "+httpurl
);
1368 httpcodeout
= geturls_text
.status_code
;
1369 httpcodereason
= geturls_text
.reason_phrase
;
1370 httpversionout
= geturls_text
.http_version
;
1371 httpmethodout
= httpmethod
;
1372 httpurlout
= str(geturls_text
.url
);
1373 httpheaderout
= geturls_text
.headers
;
1374 httpheadersentout
= geturls_text
.request
.headers
;
1375 elif(httplibuse
=="httpx2"):
1377 if(httpmethod
=="GET"):
1378 httpx_pool
= httpx
.Client(http1
=True, http2
=True, trust_env
=True);
1379 geturls_text
= httpx_pool
.get(httpurl
, headers
=httpheaders
, cookies
=httpcookie
);
1380 elif(httpmethod
=="POST"):
1381 httpx_pool
= httpx
.Client(http1
=True, http2
=True, trust_env
=True);
1382 geturls_text
= httpx_pool
.post(httpurl
, data
=postdata
, headers
=httpheaders
, cookies
=httpcookie
);
1384 httpx_pool
= httpx
.Client(http1
=True, http2
=True, trust_env
=True);
1385 geturls_text
= httpx_pool
.get(httpurl
, headers
=httpheaders
, cookies
=httpcookie
);
1386 except httpx
.ConnectTimeout
:
1387 log
.info("Error With URL "+httpurl
);
1389 except httpx
.ConnectError
:
1390 log
.info("Error With URL "+httpurl
);
1392 except socket
.timeout
:
1393 log
.info("Error With URL "+httpurl
);
1395 httpcodeout
= geturls_text
.status_code
;
1396 httpcodereason
= geturls_text
.reason_phrase
;
1397 httpversionout
= geturls_text
.http_version
;
1398 httpmethodout
= httpmethod
;
1399 httpurlout
= str(geturls_text
.url
);
1400 httpheaderout
= geturls_text
.headers
;
1401 httpheadersentout
= geturls_text
.request
.headers
;
1402 elif(httplibuse
=="httpcore"):
1404 if(httpmethod
=="GET"):
1405 httpx_pool
= httpcore
.ConnectionPool(http1
=True, http2
=False);
1406 geturls_text
= httpx_pool
.request("GET", httpurl
, headers
=httpheaders
);
1407 elif(httpmethod
=="POST"):
1408 httpx_pool
= httpcore
.ConnectionPool(http1
=True, http2
=False);
1409 geturls_text
= httpx_pool
.request("GET", httpurl
, data
=postdata
, headers
=httpheaders
);
1411 httpx_pool
= httpcore
.ConnectionPool(http1
=True, http2
=False);
1412 geturls_text
= httpx_pool
.request("GET", httpurl
, headers
=httpheaders
);
1413 except httpcore
.ConnectTimeout
:
1414 log
.info("Error With URL "+httpurl
);
1416 except httpcore
.ConnectError
:
1417 log
.info("Error With URL "+httpurl
);
1419 except socket
.timeout
:
1420 log
.info("Error With URL "+httpurl
);
1422 httpcodeout
= geturls_text
.status
;
1423 httpcodereason
= http_status_to_reason(geturls_text
.status
);
1424 httpversionout
= "1.1";
1425 httpmethodout
= httpmethod
;
1426 httpurlout
= str(httpurl
);
1427 httpheaderout
= geturls_text
.headers
;
1428 httpheadersentout
= httpheaders
;
1429 elif(httplibuse
=="httpcore2"):
1431 if(httpmethod
=="GET"):
1432 httpx_pool
= httpcore
.ConnectionPool(http1
=True, http2
=True);
1433 geturls_text
= httpx_pool
.request("GET", httpurl
, headers
=httpheaders
);
1434 elif(httpmethod
=="POST"):
1435 httpx_pool
= httpcore
.ConnectionPool(http1
=True, http2
=True);
1436 geturls_text
= httpx_pool
.request("GET", httpurl
, data
=postdata
, headers
=httpheaders
);
1438 httpx_pool
= httpcore
.ConnectionPool(http1
=True, http2
=True);
1439 geturls_text
= httpx_pool
.request("GET", httpurl
, headers
=httpheaders
);
1440 except httpcore
.ConnectTimeout
:
1441 log
.info("Error With URL "+httpurl
);
1443 except httpcore
.ConnectError
:
1444 log
.info("Error With URL "+httpurl
);
1446 except socket
.timeout
:
1447 log
.info("Error With URL "+httpurl
);
1449 httpcodeout
= geturls_text
.status
;
1450 httpcodereason
= geturls_text
.reason_phrase
;
1451 httpversionout
= "1.1";
1452 httpmethodout
= httpmethod
;
1453 httpurlout
= str(httpurl
);
1454 httpheaderout
= geturls_text
.headers
;
1455 httpheadersentout
= httpheaders
;
1456 elif(httplibuse
=="mechanize"):
1457 geturls_opener
= mechanize
.Browser();
1458 if(isinstance(httpheaders
, dict)):
1459 httpheaders
= make_http_headers_from_dict_to_list(httpheaders
);
1461 geturls_opener
.addheaders
= httpheaders
;
1462 geturls_opener
.set_cookiejar(httpcookie
);
1463 geturls_opener
.set_handle_robots(False);
1464 if(postdata
is not None and not isinstance(postdata
, dict)):
1465 postdata
= urlencode(postdata
);
1467 if(httpmethod
=="GET"):
1468 geturls_text
= geturls_opener
.open(httpurl
);
1469 elif(httpmethod
=="POST"):
1470 geturls_text
= geturls_opener
.open(httpurl
, data
=postdata
);
1472 geturls_text
= geturls_opener
.open(httpurl
);
1473 except mechanize
.HTTPError
as geturls_text_error
:
1474 geturls_text
= geturls_text_error
;
1475 log
.info("Error With URL "+httpurl
);
1477 log
.info("Error With URL "+httpurl
);
1479 except socket
.timeout
:
1480 log
.info("Error With URL "+httpurl
);
1482 httpcodeout
= geturls_text
.code
;
1483 httpcodereason
= http_status_to_reason(geturls_text
.code
);
1484 httpversionout
= "1.1";
1485 httpmethodout
= httpmethod
;
1486 httpurlout
= geturls_text
.geturl();
1487 httpheaderout
= geturls_text
.info();
1488 reqhead
= geturls_opener
.request
;
1489 httpheadersentout
= reqhead
.header_items();
1490 elif(httplibuse
=="ftp"):
1491 geturls_text
= download_file_from_ftp_file(httpurl
);
1492 if(not geturls_text
):
1494 geturls_text
.seek(0, 2);
1495 downloadsize
= geturls_text
.tell();
1496 geturls_text
.seek(0, 0);
1497 elif(httplibuse
=="sftp"):
1498 geturls_text
= download_file_from_sftp_file(httpurl
);
1499 if(not geturls_text
):
1501 geturls_text
.seek(0, 2);
1502 downloadsize
= geturls_text
.tell();
1503 geturls_text
.seek(0, 0);
1504 if(downloadsize
is not None):
1505 downloadsize
= int(downloadsize
);
1506 if downloadsize
is None: downloadsize
= 0;
1509 elif(httplibuse
=="pysftp"):
1510 geturls_text
= download_file_from_pysftp_file(httpurl
);
1511 if(not geturls_text
):
1513 geturls_text
.seek(0, 2);
1514 downloadsize
= geturls_text
.tell();
1515 geturls_text
.seek(0, 0);
1518 if(isinstance(httpheaderout
, list)):
1519 httpheaderout
= dict(make_http_headers_from_list_to_dict(httpheaderout
));
1520 if(sys
.version
[0]=="2"):
1522 prehttpheaderout
= httpheaderout
;
1523 httpheaderkeys
= httpheaderout
.keys();
1524 imax
= len(httpheaderkeys
);
1528 httpheaderout
.update({httpheaderkeys
[ic
]: prehttpheaderout
[httpheaderkeys
[ic
]]});
1530 except AttributeError:
1532 httpheaderout
= fix_header_names(httpheaderout
);
1533 if(isinstance(httpheadersentout
, list)):
1534 httpheadersentout
= dict(make_http_headers_from_list_to_dict(httpheadersentout
));
1535 httpheadersentout
= fix_header_names(httpheadersentout
);
1536 if(httplibuse
=="urllib" or httplibuse
=="request" or httplibuse
=="request3" or httplibuse
=="httplib" or httplibuse
=="httplib2" or httplibuse
=="urllib3" or httplibuse
=="requests" or httplibuse
=="mechanize" or httplibuse
=="httpx" or httplibuse
=="httpx2" or httplibuse
=="httpcore" or httplibuse
=="httpcore2"):
1537 downloadsize
= httpheaderout
.get('Content-Length');
1538 if(downloadsize
is not None):
1539 downloadsize
= int(downloadsize
);
1540 if downloadsize
is None: downloadsize
= 0;
1543 log
.info("Downloading URL "+httpurl
);
1544 if(httplibuse
=="urllib" or httplibuse
=="request" or httplibuse
=="request3" or httplibuse
=="httplib" or httplibuse
=="httplib2" or httplibuse
=="urllib3" or httplibuse
=="mechanize" or httplibuse
=="httpx" or httplibuse
=="httpx2" or httplibuse
=="httpcore" or httplibuse
=="httpcore2" or httplibuse
=="ftp" or httplibuse
=="sftp" or httplibuse
=="pysftp"):
1545 with tempfile
.NamedTemporaryFile('wb+', prefix
=tmpfileprefix
, suffix
=newtmpfilesuffix
, delete
=False) as f
:
1546 tmpfilename
= f
.name
;
1548 os
.utime(tmpfilename
, (time
.mktime(email
.utils
.parsedate_to_datetime(httpheaderout
.get('Last-Modified')).timetuple()), time
.mktime(email
.utils
.parsedate_to_datetime(httpheaderout
.get('Last-Modified')).timetuple())));
1549 except AttributeError:
1551 os
.utime(tmpfilename
, (time
.mktime(datetime
.datetime
.strptime(httpheaderout
.get('Last-Modified'), "%a, %d %b %Y %H:%M:%S %Z").timetuple()), time
.mktime(datetime
.datetime
.strptime(httpheaderout
.get('Last-Modified'), "%a, %d %b %Y %H:%M:%S %Z").timetuple())));
1556 returnval
= {'Type': "File", 'Filename': tmpfilename
, 'Filesize': downloadsize
, 'FilesizeAlt': {'IEC': get_readable_size(downloadsize
, 2, "IEC"), 'SI': get_readable_size(downloadsize
, 2, "SI")}, 'Headers': httpheaderout
, 'Version': httpversionout
, 'Method': httpmethodout
, 'HeadersSent': httpheadersentout
, 'URL': httpurlout
, 'Code': httpcodeout
, 'Reason': httpcodereason
};
1558 databytes
= geturls_text
.read(buffersize
);
1559 if not databytes
: break;
1560 datasize
= len(databytes
);
1561 fulldatasize
= datasize
+ fulldatasize
;
1564 percentage
= str("{0:.2f}".format(float(float(fulldatasize
/ downloadsize
) * 100))).rstrip('0').rstrip('.')+"%";
1565 downloaddiff
= fulldatasize
- prevdownsize
;
1566 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']);
1567 prevdownsize
= fulldatasize
;
1570 elif(httplibuse
=="requests"):
1571 with tempfile
.NamedTemporaryFile('wb+', prefix
=tmpfileprefix
, suffix
=newtmpfilesuffix
, delete
=False) as f
:
1572 tmpfilename
= f
.name
;
1574 os
.utime(tmpfilename
, (time
.mktime(email
.utils
.parsedate_to_datetime(httpheaderout
.get('Last-Modified')).timetuple()), time
.mktime(email
.utils
.parsedate_to_datetime(httpheaderout
.get('Last-Modified')).timetuple())));
1575 except AttributeError:
1577 os
.utime(tmpfilename
, (time
.mktime(datetime
.datetime
.strptime(httpheaderout
.get('Last-Modified'), "%a, %d %b %Y %H:%M:%S %Z").timetuple()), time
.mktime(datetime
.datetime
.strptime(httpheaderout
.get('Last-Modified'), "%a, %d %b %Y %H:%M:%S %Z").timetuple())));
1582 returnval
= {'Type': "File", 'Filename': tmpfilename
, 'Filesize': downloadsize
, 'FilesizeAlt': {'IEC': get_readable_size(downloadsize
, 2, "IEC"), 'SI': get_readable_size(downloadsize
, 2, "SI")}, 'Headers': httpheaderout
, 'Version': httpversionout
, 'Method': httpmethodout
, 'HeadersSent': httpheadersentout
, 'URL': httpurlout
, 'Code': httpcodeout
, 'Reason': httpcodereason
};
1583 for databytes
in geturls_text
.iter_content(chunk_size
=buffersize
):
1584 datasize
= len(databytes
);
1585 fulldatasize
= datasize
+ fulldatasize
;
1588 percentage
= str("{0:.2f}".format(float(float(fulldatasize
/ downloadsize
) * 100))).rstrip('0').rstrip('.')+"%";
1589 downloaddiff
= fulldatasize
- prevdownsize
;
1590 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']);
1591 prevdownsize
= fulldatasize
;
1596 geturls_text
.close();
1597 exec_time_end
= time
.time();
1598 log
.info("It took "+hms_string(exec_time_start
- exec_time_end
)+" to download file.");
1599 returnval
.update({'Filesize': os
.path
.getsize(tmpfilename
), 'DownloadTime': float(exec_time_start
- exec_time_end
), 'DownloadTimeReadable': hms_string(exec_time_start
- exec_time_end
)});
1602 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(), buffersize
=[524288, 524288], sleep
=-1):
1603 global geturls_download_sleep
, haverequests
, havemechanize
, havehttplib2
, haveurllib3
, havehttpx
, havehttpcore
, haveparamiko
, havepysftp
;
1605 sleep
= geturls_download_sleep
;
1606 if(httplibuse
=="urllib1" or httplibuse
=="urllib2"):
1607 httplibuse
= "urllib";
1608 if(httplibuse
=="httplib1"):
1609 httplibuse
= "httplib";
1610 if(not haverequests
and httplibuse
=="requests"):
1611 httplibuse
= "urllib";
1612 if(not havehttpx
and httplibuse
=="httpx"):
1613 httplibuse
= "urllib";
1614 if(not havehttpx
and httplibuse
=="httpx2"):
1615 httplibuse
= "urllib";
1616 if(not havehttpcore
and httplibuse
=="httpcore"):
1617 httplibuse
= "urllib";
1618 if(not havehttpcore
and httplibuse
=="httpcore2"):
1619 httplibuse
= "urllib";
1620 if(not havemechanize
and httplibuse
=="mechanize"):
1621 httplibuse
= "urllib";
1622 if(not havehttplib2
and httplibuse
=="httplib2"):
1623 httplibuse
= "httplib";
1624 if(not haveparamiko
and httplibuse
=="sftp"):
1626 if(not havepysftp
and httplibuse
=="pysftp"):
1628 if(not outfile
=="-"):
1629 outpath
= outpath
.rstrip(os
.path
.sep
);
1630 filepath
= os
.path
.realpath(outpath
+os
.path
.sep
+outfile
);
1631 if(not os
.path
.exists(outpath
)):
1632 os
.makedirs(outpath
);
1633 if(os
.path
.exists(outpath
) and os
.path
.isfile(outpath
)):
1635 if(os
.path
.exists(filepath
) and os
.path
.isdir(filepath
)):
1637 pretmpfilename
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, httplibuse
, buffersize
[0], sleep
);
1638 if(not pretmpfilename
):
1640 tmpfilename
= pretmpfilename
['Filename'];
1641 downloadsize
= os
.path
.getsize(tmpfilename
);
1643 log
.info("Moving file "+tmpfilename
+" to "+filepath
);
1644 exec_time_start
= time
.time();
1645 shutil
.move(tmpfilename
, filepath
);
1647 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())));
1648 except AttributeError:
1650 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())));
1655 exec_time_end
= time
.time();
1656 log
.info("It took "+hms_string(exec_time_start
- exec_time_end
)+" to move file.");
1657 if(os
.path
.exists(tmpfilename
)):
1658 os
.remove(tmpfilename
);
1659 returnval
= {'Type': "File", 'Filename': filepath
, 'Filesize': downloadsize
, 'FilesizeAlt': {'IEC': get_readable_size(downloadsize
, 2, "IEC"), 'SI': get_readable_size(downloadsize
, 2, "SI")}, 'DownloadTime': pretmpfilename
['DownloadTime'], 'DownloadTimeReadable': pretmpfilename
['DownloadTimeReadable'], 'MoveFileTime': float(exec_time_start
- exec_time_end
), 'MoveFileTimeReadable': hms_string(exec_time_start
- exec_time_end
), 'Headers': pretmpfilename
['Headers'], 'Version': pretmpfilename
['Version'], 'Method': pretmpfilename
['Method'], 'Method': httpmethod
, 'HeadersSent': pretmpfilename
['HeadersSent'], 'URL': pretmpfilename
['URL'], 'Code': pretmpfilename
['Code'], 'Reason': pretmpfilename
['Reason']};
1660 if(outfile
=="-" and sys
.version
[0]=="2"):
1661 pretmpfilename
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, httplibuse
, buffersize
[0], sleep
);
1662 if(not pretmpfilename
):
1664 tmpfilename
= pretmpfilename
['Filename'];
1665 downloadsize
= os
.path
.getsize(tmpfilename
);
1668 exec_time_start
= time
.time();
1669 with
open(tmpfilename
, 'rb') as ft
:
1672 databytes
= ft
.read(buffersize
[1]);
1673 if not databytes
: break;
1674 datasize
= len(databytes
);
1675 fulldatasize
= datasize
+ fulldatasize
;
1678 percentage
= str("{0:.2f}".format(float(float(fulldatasize
/ downloadsize
) * 100))).rstrip('0').rstrip('.')+"%";
1679 downloaddiff
= fulldatasize
- prevdownsize
;
1680 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']);
1681 prevdownsize
= fulldatasize
;
1684 fdata
= f
.getvalue();
1687 os
.remove(tmpfilename
);
1688 exec_time_end
= time
.time();
1689 log
.info("It took "+hms_string(exec_time_start
- exec_time_end
)+" to copy file.");
1690 returnval
= {'Type': "Content", 'Content': fdata
, 'Contentsize': downloadsize
, 'ContentsizeAlt': {'IEC': get_readable_size(downloadsize
, 2, "IEC"), 'SI': get_readable_size(downloadsize
, 2, "SI")}, 'DownloadTime': pretmpfilename
['DownloadTime'], 'DownloadTimeReadable': pretmpfilename
['DownloadTimeReadable'], 'MoveFileTime': float(exec_time_start
- exec_time_end
), 'MoveFileTimeReadable': hms_string(exec_time_start
- exec_time_end
), 'Headers': pretmpfilename
['Headers'], 'Version': pretmpfilename
['Version'], 'Method': pretmpfilename
['Method'], 'Method': httpmethod
, 'HeadersSent': pretmpfilename
['HeadersSent'], 'URL': pretmpfilename
['URL'], 'Code': pretmpfilename
['Code'], 'Reason': pretmpfilename
['Reason']};
1691 if(outfile
=="-" and sys
.version
[0]>="3"):
1692 pretmpfilename
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, httplibuse
, buffersize
[0], sleep
);
1693 tmpfilename
= pretmpfilename
['Filename'];
1694 downloadsize
= os
.path
.getsize(tmpfilename
);
1697 exec_time_start
= time
.time();
1698 with
open(tmpfilename
, 'rb') as ft
:
1701 databytes
= ft
.read(buffersize
[1]);
1702 if not databytes
: break;
1703 datasize
= len(databytes
);
1704 fulldatasize
= datasize
+ fulldatasize
;
1707 percentage
= str("{0:.2f}".format(float(float(fulldatasize
/ downloadsize
) * 100))).rstrip('0').rstrip('.')+"%";
1708 downloaddiff
= fulldatasize
- prevdownsize
;
1709 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']);
1710 prevdownsize
= fulldatasize
;
1713 fdata
= f
.getvalue();
1716 os
.remove(tmpfilename
);
1717 exec_time_end
= time
.time();
1718 log
.info("It took "+hms_string(exec_time_start
- exec_time_end
)+" to copy file.");
1719 returnval
= {'Type': "Content", 'Content': fdata
, 'Contentsize': downloadsize
, 'ContentsizeAlt': {'IEC': get_readable_size(downloadsize
, 2, "IEC"), 'SI': get_readable_size(downloadsize
, 2, "SI")}, 'DownloadTime': pretmpfilename
['DownloadTime'], 'DownloadTimeReadable': pretmpfilename
['DownloadTimeReadable'], 'MoveFileTime': float(exec_time_start
- exec_time_end
), 'MoveFileTimeReadable': hms_string(exec_time_start
- exec_time_end
), 'Headers': pretmpfilename
['Headers'], 'Version': pretmpfilename
['Version'], 'Method': pretmpfilename
['Method'], 'Method': httpmethod
, 'HeadersSent': pretmpfilename
['HeadersSent'], 'URL': pretmpfilename
['URL'], 'Code': pretmpfilename
['Code'], 'Reason': pretmpfilename
['Reason']};
1722 def download_from_url_with_urllib(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, sleep
=-1):
1723 returnval
= download_from_url(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "urllib", sleep
);
1726 def download_from_url_with_request(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, sleep
=-1):
1727 returnval
= download_from_url(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "request", sleep
);
1730 def download_from_url_with_request3(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, sleep
=-1):
1731 returnval
= download_from_url(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "request3", sleep
);
1734 def download_from_url_with_httplib(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, sleep
=-1):
1735 returnval
= download_from_url(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "httplib", sleep
);
1738 def download_from_url_with_httplib2(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, sleep
=-1):
1739 returnval
= download_from_url(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "httplib2", sleep
);
1742 def download_from_url_with_urllib3(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, sleep
=-1):
1743 returnval
= download_from_url(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "urllib3", sleep
);
1746 def download_from_url_with_requests(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, sleep
=-1):
1747 returnval
= download_from_url(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "requests", sleep
);
1750 def download_from_url_with_httpx(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, sleep
=-1):
1751 returnval
= download_from_url(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "httpx", sleep
);
1754 def download_from_url_with_httpx2(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, sleep
=-1):
1755 returnval
= download_from_url(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "httpx2", sleep
);
1758 def download_from_url_with_httpcore(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, sleep
=-1):
1759 returnval
= download_from_url(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "httpcore", sleep
);
1762 def download_from_url_with_httpcore2(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, sleep
=-1):
1763 returnval
= download_from_url(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "httpcore2", sleep
);
1766 def download_from_url_with_mechanize(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, sleep
=-1):
1767 returnval
= download_from_url(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "mechanize", sleep
);
1770 def download_from_url_with_ftp(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, sleep
=-1):
1771 returnval
= download_from_url(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "ftp", sleep
);
1774 def download_from_url_with_sftp(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, sleep
=-1):
1775 returnval
= download_from_url(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "sftp", sleep
);
1778 def download_from_url_with_pysftp(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, sleep
=-1):
1779 returnval
= download_from_url(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "pysftp", sleep
);
1782 def download_from_url_file_with_urllib(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, buffersize
=524288, sleep
=-1):
1783 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "urllib", buffersize
, sleep
);
1786 def download_from_url_file_with_request(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, buffersize
=524288, sleep
=-1):
1787 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "request", buffersize
, sleep
);
1790 def download_from_url_file_with_request3(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, buffersize
=524288, sleep
=-1):
1791 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "request3", buffersize
, sleep
);
1794 def download_from_url_file_with_httplib(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, buffersize
=524288, sleep
=-1):
1795 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "httplib", buffersize
, sleep
);
1798 def download_from_url_file_with_httplib2(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, buffersize
=524288, sleep
=-1):
1799 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "httplib2", buffersize
, sleep
);
1802 def download_from_url_file_with_urllib3(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, buffersize
=524288, sleep
=-1):
1803 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "urllib3", buffersize
, sleep
);
1806 def download_from_url_file_with_requests(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, buffersize
=524288, sleep
=-1):
1807 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "requests", buffersize
, sleep
);
1810 def download_from_url_file_with_httpx(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, buffersize
=524288, sleep
=-1):
1811 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "httpx", buffersize
, sleep
);
1814 def download_from_url_file_with_httpx2(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, buffersize
=524288, sleep
=-1):
1815 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "httpx2", buffersize
, sleep
);
1818 def download_from_url_file_with_httpcore(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, buffersize
=524288, sleep
=-1):
1819 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "httpcore", buffersize
, sleep
);
1822 def download_from_url_file_with_httpcore2(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, buffersize
=524288, sleep
=-1):
1823 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "httpcore2", buffersize
, sleep
);
1826 def download_from_url_file_with_mechanize(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, buffersize
=524288, sleep
=-1):
1827 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "mechanize", buffersize
, sleep
);
1830 def download_from_url_file_with_ftp(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, buffersize
=524288, sleep
=-1):
1831 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "ftp", buffersize
, sleep
);
1834 def download_from_url_file_with_sftp(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, buffersize
=524288, sleep
=-1):
1835 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "sftp", buffersize
, sleep
);
1838 def download_from_url_file_with_pysftp(httpurl
, httpheaders
=geturls_headers
, httpuseragent
=None, httpreferer
=None, httpcookie
=geturls_cj
, httpmethod
="GET", postdata
=None, buffersize
=524288, sleep
=-1):
1839 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "pysftp", buffersize
, sleep
);
1842 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(), buffersize
=[524288, 524288], sleep
=-1):
1843 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "urllib", outfile
, outpath
, buffersize
, sleep
);
1846 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(), buffersize
=[524288, 524288], sleep
=-1):
1847 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "request", outfile
, outpath
, buffersize
, sleep
);
1850 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(), buffersize
=[524288, 524288], sleep
=-1):
1851 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "request3", outfile
, outpath
, buffersize
, sleep
);
1854 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(), buffersize
=[524288, 524288], sleep
=-1):
1855 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "httplib", outfile
, outpath
, buffersize
, sleep
);
1858 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(), buffersize
=[524288, 524288], sleep
=-1):
1859 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "httplib2", outfile
, outpath
, buffersize
, sleep
);
1862 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(), buffersize
=[524288, 524288], sleep
=-1):
1863 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "urllib3", outfile
, outpath
, buffersize
, sleep
);
1866 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(), buffersize
=[524288, 524288], sleep
=-1):
1867 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "requests", outfile
, outpath
, buffersize
, sleep
);
1870 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(), buffersize
=[524288, 524288], sleep
=-1):
1871 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "httpx", outfile
, outpath
, buffersize
, sleep
);
1874 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(), buffersize
=[524288, 524288], sleep
=-1):
1875 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "httpx2", outfile
, outpath
, buffersize
, sleep
);
1878 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(), buffersize
=[524288, 524288], sleep
=-1):
1879 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "httpcore", outfile
, outpath
, buffersize
, sleep
);
1882 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(), buffersize
=[524288, 524288], sleep
=-1):
1883 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "httpcore2", outfile
, outpath
, buffersize
, sleep
);
1886 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(), buffersize
=[524288, 524288], sleep
=-1):
1887 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "mechanize", outfile
, outpath
, buffersize
, sleep
);
1890 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(), buffersize
=[524288, 524288], sleep
=-1):
1891 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "ftp", outfile
, outpath
, buffersize
, sleep
);
1894 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(), buffersize
=[524288, 524288], sleep
=-1):
1895 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "sftp", outfile
, outpath
, buffersize
, sleep
);
1898 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(), buffersize
=[524288, 524288], sleep
=-1):
1899 returnval
= download_from_url_file(httpurl
, httpheaders
, httpuseragent
, httpreferer
, httpcookie
, httpmethod
, postdata
, "pysftp", outfile
, outpath
, buffersize
, sleep
);
1902 def download_file_from_ftp_file(url
):
1903 urlparts
= urlparse
.urlparse(url
);
1904 file_name
= os
.path
.basename(urlparts
.path
);
1905 file_dir
= os
.path
.dirname(urlparts
.path
);
1906 if(urlparts
.username
is not None):
1907 ftp_username
= urlparts
.username
;
1909 ftp_username
= "anonymous";
1910 if(urlparts
.password
is not None):
1911 ftp_password
= urlparts
.password
;
1912 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
1913 ftp_password
= "anonymous";
1916 if(urlparts
.scheme
=="ftp"):
1918 elif(urlparts
.scheme
=="ftps"):
1922 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
1924 ftp_port
= urlparts
.port
;
1925 if(urlparts
.port
is None):
1928 ftp
.connect(urlparts
.hostname
, ftp_port
);
1929 except socket
.gaierror
:
1930 log
.info("Error With URL "+httpurl
);
1932 except socket
.timeout
:
1933 log
.info("Error With URL "+httpurl
);
1935 ftp
.login(urlparts
.username
, urlparts
.password
);
1936 if(urlparts
.scheme
=="ftps"):
1938 ftpfile
= BytesIO();
1939 ftp
.retrbinary("RETR "+urlparts
.path
, ftpfile
.write
);
1940 #ftp.storbinary("STOR "+urlparts.path, ftpfile.write);
1945 def download_file_from_ftp_string(url
):
1946 ftpfile
= download_file_from_ftp_file(url
);
1947 return ftpfile
.read();
1949 def upload_file_to_ftp_file(ftpfile
, url
):
1950 urlparts
= urlparse
.urlparse(url
);
1951 file_name
= os
.path
.basename(urlparts
.path
);
1952 file_dir
= os
.path
.dirname(urlparts
.path
);
1953 if(urlparts
.username
is not None):
1954 ftp_username
= urlparts
.username
;
1956 ftp_username
= "anonymous";
1957 if(urlparts
.password
is not None):
1958 ftp_password
= urlparts
.password
;
1959 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
1960 ftp_password
= "anonymous";
1963 if(urlparts
.scheme
=="ftp"):
1965 elif(urlparts
.scheme
=="ftps"):
1969 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
1971 ftp_port
= urlparts
.port
;
1972 if(urlparts
.port
is None):
1975 ftp
.connect(urlparts
.hostname
, ftp_port
);
1976 except socket
.gaierror
:
1977 log
.info("Error With URL "+httpurl
);
1979 except socket
.timeout
:
1980 log
.info("Error With URL "+httpurl
);
1982 ftp
.login(urlparts
.username
, urlparts
.password
);
1983 if(urlparts
.scheme
=="ftps"):
1985 ftp
.storbinary("STOR "+urlparts
.path
, ftpfile
);
1990 def upload_file_to_ftp_string(ftpstring
, url
):
1991 ftpfileo
= BytesIO(ftpstring
);
1992 ftpfile
= upload_file_to_ftp_file(ftpfileo
, url
);
1997 def download_file_from_sftp_file(url
):
1998 urlparts
= urlparse
.urlparse(url
);
1999 file_name
= os
.path
.basename(urlparts
.path
);
2000 file_dir
= os
.path
.dirname(urlparts
.path
);
2001 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
2003 sftp_port
= urlparts
.port
;
2004 if(urlparts
.port
is None):
2007 sftp_port
= urlparts
.port
;
2008 if(urlparts
.username
is not None):
2009 sftp_username
= urlparts
.username
;
2011 sftp_username
= "anonymous";
2012 if(urlparts
.password
is not None):
2013 sftp_password
= urlparts
.password
;
2014 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
2015 sftp_password
= "anonymous";
2018 if(urlparts
.scheme
!="sftp"):
2020 ssh
= paramiko
.SSHClient();
2021 ssh
.load_system_host_keys();
2022 ssh
.set_missing_host_key_policy(paramiko
.AutoAddPolicy());
2024 ssh
.connect(urlparts
.hostname
, port
=sftp_port
, username
=urlparts
.username
, password
=urlparts
.password
);
2025 except paramiko
.ssh_exception
.SSHException
:
2027 except socket
.gaierror
:
2028 log
.info("Error With URL "+httpurl
);
2030 except socket
.timeout
:
2031 log
.info("Error With URL "+httpurl
);
2033 sftp
= ssh
.open_sftp();
2034 sftpfile
= BytesIO();
2035 sftp
.getfo(urlparts
.path
, sftpfile
);
2038 sftpfile
.seek(0, 0);
2041 def download_file_from_sftp_file(url
):
2045 def download_file_from_sftp_string(url
):
2046 sftpfile
= download_file_from_sftp_file(url
);
2047 return sftpfile
.read();
2049 def download_file_from_ftp_string(url
):
2053 def upload_file_to_sftp_file(sftpfile
, url
):
2054 urlparts
= urlparse
.urlparse(url
);
2055 file_name
= os
.path
.basename(urlparts
.path
);
2056 file_dir
= os
.path
.dirname(urlparts
.path
);
2057 sftp_port
= urlparts
.port
;
2058 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
2060 if(urlparts
.port
is None):
2063 sftp_port
= urlparts
.port
;
2064 if(urlparts
.username
is not None):
2065 sftp_username
= urlparts
.username
;
2067 sftp_username
= "anonymous";
2068 if(urlparts
.password
is not None):
2069 sftp_password
= urlparts
.password
;
2070 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
2071 sftp_password
= "anonymous";
2074 if(urlparts
.scheme
!="sftp"):
2076 ssh
= paramiko
.SSHClient();
2077 ssh
.load_system_host_keys();
2078 ssh
.set_missing_host_key_policy(paramiko
.AutoAddPolicy());
2080 ssh
.connect(urlparts
.hostname
, port
=sftp_port
, username
=urlparts
.username
, password
=urlparts
.password
);
2081 except paramiko
.ssh_exception
.SSHException
:
2083 except socket
.gaierror
:
2084 log
.info("Error With URL "+httpurl
);
2086 except socket
.timeout
:
2087 log
.info("Error With URL "+httpurl
);
2089 sftp
= ssh
.open_sftp();
2090 sftp
.putfo(sftpfile
, urlparts
.path
);
2093 sftpfile
.seek(0, 0);
2096 def upload_file_to_sftp_file(sftpfile
, url
):
2100 def upload_file_to_sftp_string(sftpstring
, url
):
2101 sftpfileo
= BytesIO(sftpstring
);
2102 sftpfile
= upload_file_to_sftp_files(ftpfileo
, url
);
2106 def upload_file_to_sftp_string(url
):
2111 def download_file_from_pysftp_file(url
):
2112 urlparts
= urlparse
.urlparse(url
);
2113 file_name
= os
.path
.basename(urlparts
.path
);
2114 file_dir
= os
.path
.dirname(urlparts
.path
);
2115 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
2117 sftp_port
= urlparts
.port
;
2118 if(urlparts
.port
is None):
2121 sftp_port
= urlparts
.port
;
2122 if(urlparts
.username
is not None):
2123 sftp_username
= urlparts
.username
;
2125 sftp_username
= "anonymous";
2126 if(urlparts
.password
is not None):
2127 sftp_password
= urlparts
.password
;
2128 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
2129 sftp_password
= "anonymous";
2132 if(urlparts
.scheme
!="sftp"):
2135 pysftp
.Connection(urlparts
.hostname
, port
=sftp_port
, username
=urlparts
.username
, password
=urlparts
.password
);
2136 except paramiko
.ssh_exception
.SSHException
:
2138 except socket
.gaierror
:
2139 log
.info("Error With URL "+httpurl
);
2141 except socket
.timeout
:
2142 log
.info("Error With URL "+httpurl
);
2144 sftp
= ssh
.open_sftp();
2145 sftpfile
= BytesIO();
2146 sftp
.getfo(urlparts
.path
, sftpfile
);
2149 sftpfile
.seek(0, 0);
2152 def download_file_from_pysftp_file(url
):
2156 def download_file_from_pysftp_string(url
):
2157 sftpfile
= download_file_from_pysftp_file(url
);
2158 return sftpfile
.read();
2160 def download_file_from_ftp_string(url
):
2164 def upload_file_to_pysftp_file(sftpfile
, url
):
2165 urlparts
= urlparse
.urlparse(url
);
2166 file_name
= os
.path
.basename(urlparts
.path
);
2167 file_dir
= os
.path
.dirname(urlparts
.path
);
2168 sftp_port
= urlparts
.port
;
2169 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
2171 if(urlparts
.port
is None):
2174 sftp_port
= urlparts
.port
;
2175 if(urlparts
.username
is not None):
2176 sftp_username
= urlparts
.username
;
2178 sftp_username
= "anonymous";
2179 if(urlparts
.password
is not None):
2180 sftp_password
= urlparts
.password
;
2181 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
2182 sftp_password
= "anonymous";
2185 if(urlparts
.scheme
!="sftp"):
2188 pysftp
.Connection(urlparts
.hostname
, port
=sftp_port
, username
=urlparts
.username
, password
=urlparts
.password
);
2189 except paramiko
.ssh_exception
.SSHException
:
2191 except socket
.gaierror
:
2192 log
.info("Error With URL "+httpurl
);
2194 except socket
.timeout
:
2195 log
.info("Error With URL "+httpurl
);
2197 sftp
= ssh
.open_sftp();
2198 sftp
.putfo(sftpfile
, urlparts
.path
);
2201 sftpfile
.seek(0, 0);
2204 def upload_file_to_pysftp_file(sftpfile
, url
):
2208 def upload_file_to_pysftp_string(sftpstring
, url
):
2209 sftpfileo
= BytesIO(sftpstring
);
2210 sftpfile
= upload_file_to_pysftp_files(ftpfileo
, url
);
2214 def upload_file_to_pysftp_string(url
):