From 44bd7dc4432dc6f0550d91a5948bd9ad3927447d Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Fri, 16 Aug 2024 15:46:50 +0300 Subject: [PATCH] Feat(Robots): Removed connect_timeout, added ftp_timeout --- Robots/bkmk_raiohttp.py | 13 ++++++------- Robots/bkmk_rcurl.py | 8 ++++++-- Robots/bkmk_robot_base.py | 4 ++-- doc/ANNOUNCE | 2 ++ doc/ChangeLog | 2 ++ 5 files changed, 18 insertions(+), 11 deletions(-) diff --git a/Robots/bkmk_raiohttp.py b/Robots/bkmk_raiohttp.py index df47aa4..422db04 100644 --- a/Robots/bkmk_raiohttp.py +++ b/Robots/bkmk_raiohttp.py @@ -28,8 +28,7 @@ class robot_aiohttp(robot_base): def get(self, bookmark, url, accept_charset=False, use_proxy=False): if url.startswith('ftp://'): error, body = asyncio.run(_get_ftp( - url, connect_timeout=self.connect_timeout, - timeout=self.timeout, + url, timeout=self.ftp_timeout, )) if error is not None: error = str(error) @@ -49,7 +48,7 @@ class robot_aiohttp(robot_base): error, status, resp_headers, body = asyncio.run(get_http( url, headers=headers, proxy=proxy, - connect_timeout=self.connect_timeout, timeout=self.timeout, + timeout=self.timeout, )) if error is not None or (status and status >= 400): if error is None: @@ -67,8 +66,8 @@ class robot_aiohttp(robot_base): return '' # We don't store welcome message yet -async def get_http(url, headers={}, proxy=None, connect_timeout=30, timeout=60): - timeout = aiohttp.ClientTimeout(connect=connect_timeout, total=timeout) +async def get_http(url, headers={}, proxy=None, timeout=60): + timeout = aiohttp.ClientTimeout(connect=timeout, total=timeout) try: async with aiohttp.ClientSession(timeout=timeout) as session: async with session.get( @@ -79,7 +78,7 @@ async def get_http(url, headers={}, proxy=None, connect_timeout=30, timeout=60): return e, None, None, None -async def _get_ftp(url, connect_timeout=30, timeout=60): +async def _get_ftp(url, timeout=60): split_results = urlsplit(url) path = split_results.path or '/' user = split_results.username or 'anonymous' @@ -91,7 +90,7 @@ async def _get_ftp(url, connect_timeout=30, timeout=60): try: async with aioftp.Client.context( host, port, user=user, password=password, - socket_timeout=connect_timeout, path_timeout=timeout, + socket_timeout=timeout, path_timeout=timeout, ) as client: async for _path, _info in client.list(path): lines.append('%s %s' % (_info, _path)) diff --git a/Robots/bkmk_rcurl.py b/Robots/bkmk_rcurl.py index bf32970..a994176 100644 --- a/Robots/bkmk_rcurl.py +++ b/Robots/bkmk_rcurl.py @@ -42,8 +42,12 @@ class robot_curl(robot_base): curl.setopt(pycurl.SSL_VERIFYHOST, 2) curl.setopt(curl.CAINFO, certifi.where()) # Set timeouts to avoid hanging too long - curl.setopt(pycurl.CONNECTTIMEOUT, self.connect_timeout) - curl.setopt(pycurl.TIMEOUT, self.timeout) + if url.startswith('ftp://'): + timeout = self.ftp_timeout + else: + timeout = self.timeout + curl.setopt(pycurl.CONNECTTIMEOUT, timeout) + curl.setopt(pycurl.TIMEOUT, timeout) # Parse Last-Modified curl.setopt(pycurl.OPT_FILETIME, 1) diff --git a/Robots/bkmk_robot_base.py b/Robots/bkmk_robot_base.py index a8a15d5..ddece90 100644 --- a/Robots/bkmk_robot_base.py +++ b/Robots/bkmk_robot_base.py @@ -74,11 +74,11 @@ class robot_base(Robot): # ...but aren't accessible even through proxy proxy_error = set() - connect_timeout = 30 timeout = 60 + ftp_timeout = 60 def __init__(self, *args, **kw): - for attr in 'connect_timeout', 'timeout': + for attr in 'timeout', 'ftp_timeout': value = getattr(self, attr) if not isinstance(value, int): value = int(value) diff --git a/doc/ANNOUNCE b/doc/ANNOUNCE index 3b7d9a3..e97f411 100644 --- a/doc/ANNOUNCE +++ b/doc/ANNOUNCE @@ -11,6 +11,8 @@ Version 5.7.0 (2024-??-??) Robot bkmk_rrequests: Use ftplib directly, without requests_ftp. + Robots: Removed connect_timeout, added ftp_timeout. + Version 5.6.1 (2024-08-15) Minor fixes. diff --git a/doc/ChangeLog b/doc/ChangeLog index f213e72..4bb123a 100644 --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -2,6 +2,8 @@ Version 5.7.0 (2024-??-??) Robot bkmk_rrequests: Use ftplib directly, without requests_ftp. + Robots: Removed connect_timeout, added ftp_timeout. + Version 5.6.1 (2024-08-15) Minor fixes. -- 2.39.5