-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpywwwgetmini.py
690 lines (629 loc) · 25.1 KB
/
pywwwgetmini.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
This program is free software; you can redistribute it and/or modify
it under the terms of the Revised BSD License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Revised BSD License for more details.
Copyright 2015-2024 Game Maker 2k - https://github.com/GameMaker2k
Copyright 2015-2024 Kazuki Przyborowski - https://github.com/KazukiPrzyborowski
$FileInfo: pywwwget.py - Last Update: 10/22/2024 Ver. 2.1.0 RC 1 - Author: cooldude2k $
'''
from __future__ import absolute_import, division, print_function, unicode_literals, generators, with_statement, nested_scopes
import os
import socket
import shutil
import logging
import platform
from .versioninfo import (__program_alt_name__, __program_name__, __project__,
__project_release_url__, __project_url__,
__revision__, __revision_id__, __version__,
__version_date__, __version_date_info__,
__version_date_plusrc__, __version_info__)
# FTP Support
ftpssl = True
try:
from ftplib import FTP, FTP_TLS
except ImportError:
ftpssl = False
from ftplib import FTP
try:
basestring
except NameError:
basestring = str
# URL Parsing
try:
from urllib.parse import urlparse, urlunparse
except ImportError:
from urlparse import urlparse, urlunparse
# Paramiko support
haveparamiko = False
try:
import paramiko
haveparamiko = True
except ImportError:
pass
# PySFTP support
havepysftp = False
try:
import pysftp
havepysftp = True
except ImportError:
pass
# Add the mechanize import check
havemechanize = False
try:
import mechanize
havemechanize = True
except ImportError:
pass
# Requests support
haverequests = False
try:
import requests
haverequests = True
import urllib3
logging.getLogger("urllib3").setLevel(logging.WARNING)
except ImportError:
pass
# HTTPX support
havehttpx = False
try:
import httpx
havehttpx = True
logging.getLogger("httpx").setLevel(logging.WARNING)
logging.getLogger("httpcore").setLevel(logging.WARNING)
except ImportError:
pass
# HTTP and URL parsing
try:
from urllib.request import Request, build_opener, HTTPBasicAuthHandler
from urllib.parse import urlparse
except ImportError:
from urllib2 import Request, build_opener, HTTPBasicAuthHandler
from urlparse import urlparse
# StringIO and BytesIO
try:
from io import StringIO, BytesIO
except ImportError:
try:
from cStringIO import StringIO
from cStringIO import StringIO as BytesIO
except ImportError:
from StringIO import StringIO
from StringIO import StringIO as BytesIO
__use_pysftp__ = False
if(not havepysftp):
__use_pysftp__ = False
__use_http_lib__ = "httpx"
if(__use_http_lib__ == "httpx" and haverequests and not havehttpx):
__use_http_lib__ = "requests"
if(__use_http_lib__ == "requests" and havehttpx and not haverequests):
__use_http_lib__ = "httpx"
if((__use_http_lib__ == "httpx" or __use_http_lib__ == "requests") and not havehttpx and not haverequests):
__use_http_lib__ = "urllib"
__program_name__ = "PyWWW-Get"
__program_alt_name__ = "PyWWWGet"
__program_small_name__ = "wwwget"
__project__ = __program_name__
__project_url__ = "https://github.com/GameMaker2k/PyWWW-Get"
__version_info__ = (2, 1, 0, "RC 1", 1)
__version_date_info__ = (2024, 10, 22, "RC 1", 1)
__version_date__ = str(__version_date_info__[0])+"."+str(__version_date_info__[
1]).zfill(2)+"."+str(__version_date_info__[2]).zfill(2)
__revision__ = __version_info__[3]
__revision_id__ = "$Id$"
if(__version_info__[4] is not None):
__version_date_plusrc__ = __version_date__ + \
"-"+str(__version_date_info__[4])
if(__version_info__[4] is None):
__version_date_plusrc__ = __version_date__
if(__version_info__[3] is not None):
__version__ = str(__version_info__[0])+"."+str(__version_info__[1])+"."+str(
__version_info__[2])+" "+str(__version_info__[3])
if(__version_info__[3] is None):
__version__ = str(
__version_info__[0])+"."+str(__version_info__[1])+"."+str(__version_info__[2])
PyBitness = platform.architecture()
if(PyBitness == "32bit" or PyBitness == "32"):
PyBitness = "32"
elif(PyBitness == "64bit" or PyBitness == "64"):
PyBitness = "64"
else:
PyBitness = "32"
geturls_ua_pywwwget_python = "Mozilla/5.0 (compatible; {proname}/{prover}; +{prourl})".format(
proname=__project__, prover=__version__, prourl=__project_url__)
if(platform.python_implementation() != ""):
py_implementation = platform.python_implementation()
if(platform.python_implementation() == ""):
py_implementation = "Python"
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__)
geturls_ua_googlebot_google = "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"
geturls_ua_googlebot_google_old = "Googlebot/2.1 (+http://www.google.com/bot.html)"
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)}
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)}
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"}
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"}
def download_file_from_ftp_file(url):
urlparts = urlparse(url)
file_name = os.path.basename(urlparts.path)
file_dir = os.path.dirname(urlparts.path)
if(urlparts.username is not None):
ftp_username = urlparts.username
else:
ftp_username = "anonymous"
if(urlparts.password is not None):
ftp_password = urlparts.password
elif(urlparts.password is None and urlparts.username == "anonymous"):
ftp_password = "anonymous"
else:
ftp_password = ""
if(urlparts.scheme == "ftp"):
ftp = FTP()
elif(urlparts.scheme == "ftps" and ftpssl):
ftp = FTP_TLS()
else:
return False
if(urlparts.scheme == "sftp"):
if(__use_pysftp__):
return download_file_from_pysftp_file(url)
else:
return download_file_from_sftp_file(url)
elif(urlparts.scheme == "http" or urlparts.scheme == "https"):
return download_file_from_http_file(url)
ftp_port = urlparts.port
if(urlparts.port is None):
ftp_port = 21
try:
ftp.connect(urlparts.hostname, ftp_port)
except socket.gaierror:
log.info("Error With URL "+url)
return False
except socket.timeout:
log.info("Error With URL "+url)
return False
ftp.login(urlparts.username, urlparts.password)
if(urlparts.scheme == "ftps"):
ftp.prot_p()
ftpfile = BytesIO()
ftp.retrbinary("RETR "+urlparts.path, ftpfile.write)
#ftp.storbinary("STOR "+urlparts.path, ftpfile.write);
ftp.close()
ftpfile.seek(0, 0)
return ftpfile
def download_file_from_ftp_string(url):
ftpfile = download_file_from_ftp_file(url)
return ftpfile.read()
def upload_file_to_ftp_file(ftpfile, url):
urlparts = urlparse(url)
file_name = os.path.basename(urlparts.path)
file_dir = os.path.dirname(urlparts.path)
if(urlparts.username is not None):
ftp_username = urlparts.username
else:
ftp_username = "anonymous"
if(urlparts.password is not None):
ftp_password = urlparts.password
elif(urlparts.password is None and urlparts.username == "anonymous"):
ftp_password = "anonymous"
else:
ftp_password = ""
if(urlparts.scheme == "ftp"):
ftp = FTP()
elif(urlparts.scheme == "ftps" and ftpssl):
ftp = FTP_TLS()
else:
return False
if(urlparts.scheme == "sftp"):
if(__use_pysftp__):
return upload_file_to_pysftp_file(url)
else:
return upload_file_to_sftp_file(url)
elif(urlparts.scheme == "http" or urlparts.scheme == "https"):
return False
ftp_port = urlparts.port
if(urlparts.port is None):
ftp_port = 21
try:
ftp.connect(urlparts.hostname, ftp_port)
except socket.gaierror:
log.info("Error With URL "+url)
return False
except socket.timeout:
log.info("Error With URL "+url)
return False
ftp.login(urlparts.username, urlparts.password)
if(urlparts.scheme == "ftps"):
ftp.prot_p()
ftp.storbinary("STOR "+urlparts.path, ftpfile)
ftp.close()
ftpfile.seek(0, 0)
return ftpfile
def upload_file_to_ftp_string(ftpstring, url):
ftpfileo = BytesIO(ftpstring)
ftpfile = upload_file_to_ftp_file(ftpfileo, url)
ftpfileo.close()
return ftpfile
class RawIteratorWrapper:
def __init__(self, iterator):
self.iterator = iterator
self.buffer = b""
self._iterator_exhausted = False
def read(self, size=-1):
if self._iterator_exhausted:
return b''
while size < 0 or len(self.buffer) < size:
try:
chunk = next(self.iterator)
self.buffer += chunk
except StopIteration:
self._iterator_exhausted = True
break
if size < 0:
size = len(self.buffer)
result, self.buffer = self.buffer[:size], self.buffer[size:]
return result
def download_file_from_http_file(url, headers=None, usehttp=__use_http_lib__):
if headers is None:
headers = {}
urlparts = urlparse(url)
username = urlparts.username
password = urlparts.password
# Rebuild URL without username and password
netloc = urlparts.hostname or ''
if urlparts.port:
netloc += ':' + str(urlparts.port)
rebuilt_url = urlunparse((urlparts.scheme, netloc, urlparts.path,
urlparts.params, urlparts.query, urlparts.fragment))
# Handle SFTP/FTP
if urlparts.scheme == "sftp":
if __use_pysftp__:
return download_file_from_pysftp_file(url)
else:
return download_file_from_sftp_file(url)
elif urlparts.scheme == "ftp" or urlparts.scheme == "ftps":
return download_file_from_ftp_file(url)
# Create a temporary file object
httpfile = BytesIO()
# 1) Requests branch
if usehttp == 'requests' and haverequests:
if username and password:
response = requests.get(
rebuilt_url, headers=headers, auth=(username, password), stream=True
)
else:
response = requests.get(rebuilt_url, headers=headers, stream=True)
response.raw.decode_content = True
shutil.copyfileobj(response.raw, httpfile)
# 2) HTTPX branch
elif usehttp == 'httpx' and havehttpx:
with httpx.Client(follow_redirects=True) as client:
if username and password:
response = client.get(
rebuilt_url, headers=headers, auth=(username, password)
)
else:
response = client.get(rebuilt_url, headers=headers)
raw_wrapper = RawIteratorWrapper(response.iter_bytes())
shutil.copyfileobj(raw_wrapper, httpfile)
# 3) Mechanize branch
elif usehttp == 'mechanize' and havemechanize:
# Create a mechanize browser
br = mechanize.Browser()
# Optional: configure mechanize (disable robots.txt, handle redirects, etc.)
br.set_handle_robots(False)
# If you need custom headers, add them as a list of (header_name, header_value)
if headers:
br.addheaders = list(headers.items())
# If you need to handle basic auth:
if username and password:
# Mechanize has its own password manager; this is one way to do it:
br.add_password(rebuilt_url, username, password)
# Open the URL and copy the response to httpfile
response = br.open(rebuilt_url)
shutil.copyfileobj(response, httpfile)
# 4) Fallback to urllib
else:
request = Request(rebuilt_url, headers=headers)
if username and password:
password_mgr = HTTPPasswordMgrWithDefaultRealm()
password_mgr.add_password(None, rebuilt_url, username, password)
auth_handler = HTTPBasicAuthHandler(password_mgr)
opener = build_opener(auth_handler)
else:
opener = build_opener()
response = opener.open(request)
shutil.copyfileobj(response, httpfile)
# Reset file pointer to the start before returning
httpfile.seek(0, 0)
return httpfile
def download_file_from_http_string(url, headers=geturls_headers_pywwwget_python_alt, usehttp=__use_http_lib__):
httpfile = download_file_from_http_file(url, headers, usehttp)
return httpfile.read()
if(haveparamiko):
def download_file_from_sftp_file(url):
urlparts = urlparse(url)
file_name = os.path.basename(urlparts.path)
file_dir = os.path.dirname(urlparts.path)
sftp_port = urlparts.port
if(urlparts.port is None):
sftp_port = 22
else:
sftp_port = urlparts.port
if(urlparts.username is not None):
sftp_username = urlparts.username
else:
sftp_username = "anonymous"
if(urlparts.password is not None):
sftp_password = urlparts.password
elif(urlparts.password is None and urlparts.username == "anonymous"):
sftp_password = "anonymous"
else:
sftp_password = ""
if(urlparts.scheme == "ftp"):
return download_file_from_ftp_file(url)
elif(urlparts.scheme == "http" or urlparts.scheme == "https"):
return download_file_from_http_file(url)
if(urlparts.scheme != "sftp"):
return False
ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh.connect(urlparts.hostname, port=sftp_port,
username=urlparts.username, password=urlparts.password)
except paramiko.ssh_exception.SSHException:
return False
except socket.gaierror:
log.info("Error With URL "+url)
return False
except socket.timeout:
log.info("Error With URL "+url)
return False
sftp = ssh.open_sftp()
sftpfile = BytesIO()
sftp.getfo(urlparts.path, sftpfile)
sftp.close()
ssh.close()
sftpfile.seek(0, 0)
return sftpfile
else:
def download_file_from_sftp_file(url):
return False
if(haveparamiko):
def download_file_from_sftp_string(url):
sftpfile = download_file_from_sftp_file(url)
return sftpfile.read()
else:
def download_file_from_sftp_string(url):
return False
if(haveparamiko):
def upload_file_to_sftp_file(sftpfile, url):
urlparts = urlparse(url)
file_name = os.path.basename(urlparts.path)
file_dir = os.path.dirname(urlparts.path)
sftp_port = urlparts.port
if(urlparts.port is None):
sftp_port = 22
else:
sftp_port = urlparts.port
if(urlparts.username is not None):
sftp_username = urlparts.username
else:
sftp_username = "anonymous"
if(urlparts.password is not None):
sftp_password = urlparts.password
elif(urlparts.password is None and urlparts.username == "anonymous"):
sftp_password = "anonymous"
else:
sftp_password = ""
if(urlparts.scheme == "ftp"):
return upload_file_to_ftp_file(url)
elif(urlparts.scheme == "http" or urlparts.scheme == "https"):
return False
if(urlparts.scheme != "sftp"):
return False
ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh.connect(urlparts.hostname, port=sftp_port,
username=urlparts.username, password=urlparts.password)
except paramiko.ssh_exception.SSHException:
return False
except socket.gaierror:
log.info("Error With URL "+url)
return False
except socket.timeout:
log.info("Error With URL "+url)
return False
sftp = ssh.open_sftp()
sftp.putfo(sftpfile, urlparts.path)
sftp.close()
ssh.close()
sftpfile.seek(0, 0)
return sftpfile
else:
def upload_file_to_sftp_file(sftpfile, url):
return False
if(haveparamiko):
def upload_file_to_sftp_string(sftpstring, url):
sftpfileo = BytesIO(sftpstring)
sftpfile = upload_file_to_sftp_files(ftpfileo, url)
sftpfileo.close()
return sftpfile
else:
def upload_file_to_sftp_string(url):
return False
if(havepysftp):
def download_file_from_pysftp_file(url):
urlparts = urlparse(url)
file_name = os.path.basename(urlparts.path)
file_dir = os.path.dirname(urlparts.path)
sftp_port = urlparts.port
if(urlparts.port is None):
sftp_port = 22
else:
sftp_port = urlparts.port
if(urlparts.username is not None):
sftp_username = urlparts.username
else:
sftp_username = "anonymous"
if(urlparts.password is not None):
sftp_password = urlparts.password
elif(urlparts.password is None and urlparts.username == "anonymous"):
sftp_password = "anonymous"
else:
sftp_password = ""
if(urlparts.scheme == "ftp"):
return download_file_from_ftp_file(url)
elif(urlparts.scheme == "http" or urlparts.scheme == "https"):
return download_file_from_http_file(url)
if(urlparts.scheme != "sftp"):
return False
try:
pysftp.Connection(urlparts.hostname, port=sftp_port,
username=urlparts.username, password=urlparts.password)
except paramiko.ssh_exception.SSHException:
return False
except socket.gaierror:
log.info("Error With URL "+url)
return False
except socket.timeout:
log.info("Error With URL "+url)
return False
sftp = ssh.open_sftp()
sftpfile = BytesIO()
sftp.getfo(urlparts.path, sftpfile)
sftp.close()
ssh.close()
sftpfile.seek(0, 0)
return sftpfile
else:
def download_file_from_pysftp_file(url):
return False
if(havepysftp):
def download_file_from_pysftp_string(url):
sftpfile = download_file_from_pysftp_file(url)
return sftpfile.read()
else:
def download_file_from_pyftp_string(url):
return False
if(havepysftp):
def upload_file_to_pysftp_file(sftpfile, url):
urlparts = urlparse(url)
file_name = os.path.basename(urlparts.path)
file_dir = os.path.dirname(urlparts.path)
sftp_port = urlparts.port
if(urlparts.port is None):
sftp_port = 22
else:
sftp_port = urlparts.port
if(urlparts.username is not None):
sftp_username = urlparts.username
else:
sftp_username = "anonymous"
if(urlparts.password is not None):
sftp_password = urlparts.password
elif(urlparts.password is None and urlparts.username == "anonymous"):
sftp_password = "anonymous"
else:
sftp_password = ""
if(urlparts.scheme == "ftp"):
return upload_file_to_ftp_file(url)
elif(urlparts.scheme == "http" or urlparts.scheme == "https"):
return False
if(urlparts.scheme != "sftp"):
return False
try:
pysftp.Connection(urlparts.hostname, port=sftp_port,
username=urlparts.username, password=urlparts.password)
except paramiko.ssh_exception.SSHException:
return False
except socket.gaierror:
log.info("Error With URL "+url)
return False
except socket.timeout:
log.info("Error With URL "+url)
return False
sftp = ssh.open_sftp()
sftp.putfo(sftpfile, urlparts.path)
sftp.close()
ssh.close()
sftpfile.seek(0, 0)
return sftpfile
else:
def upload_file_to_pysftp_file(sftpfile, url):
return False
if(havepysftp):
def upload_file_to_pysftp_string(sftpstring, url):
sftpfileo = BytesIO(sftpstring)
sftpfile = upload_file_to_pysftp_files(ftpfileo, url)
sftpfileo.close()
return sftpfile
else:
def upload_file_to_pysftp_string(url):
return False
def download_file_from_internet_file(url, headers=geturls_headers_pywwwget_python_alt, usehttp=__use_http_lib__):
urlparts = urlparse(url)
if(urlparts.scheme == "http" or urlparts.scheme == "https"):
return download_file_from_http_file(url, headers, usehttp)
elif(urlparts.scheme == "ftp" or urlparts.scheme == "ftps"):
return download_file_from_ftp_file(url)
elif(urlparts.scheme == "sftp"):
if(__use_pysftp__ and havepysftp):
return download_file_from_pysftp_file(url)
else:
return download_file_from_sftp_file(url)
else:
return False
return False
def download_file_from_internet_string(url, headers=geturls_headers_pywwwget_python_alt, usehttp=__use_http_lib__):
urlparts = urlparse(url)
if(urlparts.scheme == "http" or urlparts.scheme == "https"):
return download_file_from_http_string(url, headers, usehttp)
elif(urlparts.scheme == "ftp" or urlparts.scheme == "ftps"):
return download_file_from_ftp_string(url)
elif(urlparts.scheme == "sftp"):
if(__use_pysftp__ and havepysftp):
return download_file_from_pysftp_string(url)
else:
return download_file_from_sftp_string(url)
else:
return False
return False
def upload_file_to_internet_file(ifp, url):
urlparts = urlparse(url)
if(urlparts.scheme == "http" or urlparts.scheme == "https"):
return False
elif(urlparts.scheme == "ftp" or urlparts.scheme == "ftps"):
return upload_file_to_ftp_file(ifp, url)
elif(urlparts.scheme == "sftp"):
if(__use_pysftp__ and havepysftp):
return upload_file_to_pysftp_file(ifp, url)
else:
return upload_file_to_sftp_file(ifp, url)
else:
return False
return False
def upload_file_to_internet_string(ifp, url):
urlparts = urlparse(url)
if(urlparts.scheme == "http" or urlparts.scheme == "https"):
return False
elif(urlparts.scheme == "ftp" or urlparts.scheme == "ftps"):
return upload_file_to_ftp_string(ifp, url)
elif(urlparts.scheme == "sftp"):
if(__use_pysftp__ and havepysftp):
return upload_file_to_pysftp_string(ifp, url)
else:
return upload_file_to_sftp_string(ifp, url)
else:
return False
return False