__all__ = ['robot_requests']
+from urllib.parse import urlsplit
+import ftplib
+import socket
import warnings
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.ssl_ import create_urllib3_context
import requests
-import requests_ftp
import urllib3
from Robots.bkmk_robot_base import robot_base, request_headers
-requests_ftp.monkeypatch_session()
-
class robot_requests(robot_base):
def version_str(self):
return 'python-requests urllib3/%s' % urllib3.__version__
def get(self, bookmark, url, accept_charset=False, use_proxy=False):
+ if url.startswith('ftp://'):
+ error, welcome, body = _get_ftp(url, self.timeout)
+ if error is not None:
+ return error, None, None, None, None
+ self.welcome = welcome
+ return None, None, None, None, body
+
if accept_charset and bookmark.charset:
headers = request_headers.copy()
headers['Accept-Charset'] = bookmark.charset
return None, None, None, r.headers, r.content
def get_ftp_welcome(self):
- return '' # Alas, requests_ftp doesn't store welcome message
+ welcome = self.welcome
+ self.welcome = ''
+ return welcome
+
+
+def _get_ftp(url, timeout):
+ split_results = urlsplit(url)
+ path = split_results.path or '/'
+ user = split_results.username or 'anonymous'
+ password = split_results.password or 'ftp'
+ host = split_results.hostname
+ port = split_results.port or 21
+
+ ftp = ftplib.FTP()
+ try:
+ ftp.connect(host, port, timeout)
+ ftp.login(user, password)
+ if path != '/':
+ ftp.cwd(path)
+ except (socket.error, ftplib.Error) as e:
+ return "Error: %s" % e, None, None
+
+ files = []
+ try:
+ files = ftp.nlst()
+ except ftplib.error_perm as e:
+ if str(e).startswith("550 No files found"):
+ return None, ftp.welcome, ''
+ return "Error: %s" % e, None, None
+ except ftplib.Error as e:
+ return "Error: %s" % e, None, None
+
+ return None, ftp.welcome, '\n'.join(files)
# See https://lukasa.co.uk/2017/02/Configuring_TLS_With_Requests/