From a49a2b7ac170a5a7bbb73cac41364924445f0b34 Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Fri, 16 Aug 2024 01:36:07 +0300 Subject: [PATCH] Feat(bkmk_rrequests): Use ftplib directly, without requests_ftp --- Robots/bkmk_rrequests.py | 47 ++++++++++++++++++++++++++++++++++++---- bkmk_db-venv | 2 +- doc/ANNOUNCE | 4 ++++ doc/ChangeLog | 4 ++++ setup.py | 2 +- 5 files changed, 53 insertions(+), 6 deletions(-) diff --git a/Robots/bkmk_rrequests.py b/Robots/bkmk_rrequests.py index ebe90cb..a44cdd0 100644 --- a/Robots/bkmk_rrequests.py +++ b/Robots/bkmk_rrequests.py @@ -11,24 +11,31 @@ __license__ = "GNU GPL" __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 @@ -67,7 +74,39 @@ class robot_requests(robot_base): 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/ diff --git a/bkmk_db-venv b/bkmk_db-venv index 085c919..25608fc 100644 --- a/bkmk_db-venv +++ b/bkmk_db-venv @@ -8,7 +8,7 @@ if [ -z "$VIRTUAL_ENV" ]; then } && . bkmk_db-venv/bin/activate && pip install --compile --upgrade beautifulsoup4 lxml m_lib.full \ - "requests[socks]" requests-ftp \ + "requests[socks]" \ pycurl certifi \ aiohttp aioftp } diff --git a/doc/ANNOUNCE b/doc/ANNOUNCE index bb9807b..3b7d9a3 100644 --- a/doc/ANNOUNCE +++ b/doc/ANNOUNCE @@ -7,6 +7,10 @@ bookmarks.html. WHAT'S NEW +Version 5.7.0 (2024-??-??) + + Robot bkmk_rrequests: Use ftplib directly, without requests_ftp. + Version 5.6.1 (2024-08-15) Minor fixes. diff --git a/doc/ChangeLog b/doc/ChangeLog index e3c6cc6..f213e72 100644 --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -1,3 +1,7 @@ +Version 5.7.0 (2024-??-??) + + Robot bkmk_rrequests: Use ftplib directly, without requests_ftp. + Version 5.6.1 (2024-08-15) Minor fixes. diff --git a/setup.py b/setup.py index 391890e..1c3160f 100755 --- a/setup.py +++ b/setup.py @@ -38,7 +38,7 @@ setup( ], extras_require={ 'html': ['beautifulsoup4', 'lxml'], - 'requests': ['requests[socks]', 'requests-ftp'], + 'requests': ['requests[socks]'], 'curl': ['pycurl', 'certifi'], 'aiohttp': ['aiohttp', 'aioftp'], }, -- 2.39.5