__all__ = ['robot_aiohttp']
+from urllib.parse import urlsplit
import asyncio
+
+import aioftp
import aiohttp
import aiohttp.client_exceptions
+
from Robots.bkmk_robot_base import robot_base, request_headers
return 'aiohttp/%s' % aiohttp.__version__
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,
+ ))
+ if error is not None:
+ error = str(error)
+ return error, None, None, None, None
+ return None, None, None, None, body
+
if accept_charset and bookmark.charset:
headers = request_headers.copy()
headers['Accept-Charset'] = bookmark.charset
else:
proxy = None
- error, status, resp_headers, body = asyncio.run(get(
+ error, status, resp_headers, body = asyncio.run(get_http(
url, headers=headers, proxy=proxy,
connect_timeout=self.connect_timeout, timeout=self.timeout,
))
return None, status, resp_headers['Location'], None, None
return None, status, None, resp_headers, body
+ def get_ftp_welcome(self):
+ return '' # We don't store welcome message yet
+
-async def get(url, headers={}, proxy=None, connect_timeout=30, timeout=60):
+async def get_http(url, headers={}, proxy=None, connect_timeout=30, timeout=60):
timeout = aiohttp.ClientTimeout(connect=connect_timeout, total=timeout)
try:
async with aiohttp.ClientSession(timeout=timeout) as session:
return None, resp.status, resp.headers, await resp.read()
except (asyncio.TimeoutError, aiohttp.client_exceptions.ClientError) as e:
return e, None, None, None
+
+
+async def get_ftp(url, connect_timeout=30, timeout=60):
+ 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
+
+ lines = []
+ try:
+ async with aioftp.Client.context(
+ host, port, user=user, password=password,
+ socket_timeout=connect_timeout, path_timeout=timeout,
+ ) as client:
+ async for _path, _info in client.list(path, recursive=True):
+ lines.append('%s %s' % (_info, _path))
+ except (OSError, aioftp.errors.AIOFTPException) as e:
+ return e, None, None, None
+ return None, None, None, '\n'.join(lines)