Common API¶
- class aioftp.StreamIO(reader: StreamReader, writer: StreamWriter, *, timeout: float | int | None = None, read_timeout: float | int | None = None, write_timeout: float | int | None = None)¶
Stream input/output wrapper with timeout.
- Parameters:
reader (
asyncio.StreamReader) – stream readerwriter (
asyncio.StreamWriter) – stream writertimeout (
int,floatorNone) – socket timeout for read/write operationsread_timeout (
int,floatorNone) – socket timeout for read operations, overrides timeoutwrite_timeout (
int,floatorNone) – socket timeout for write operations, overrides timeout
- async read(count: int = -1) bytes¶
asyncio.coroutine()Proxy for
asyncio.StreamReader.read().- Parameters:
count (
int) – block size for read operation
- async readexactly(count: int) bytes¶
asyncio.coroutine()Proxy for
asyncio.StreamReader.readexactly().- Parameters:
count (
int) – block size for read operation
- async readline() bytes¶
asyncio.coroutine()Proxy for
asyncio.StreamReader.readline().
- async start_tls(sslcontext: SSLContext, server_hostname: str | None) None¶
Upgrades the connection to TLS
- async write(data: bytes) None¶
asyncio.coroutine()Combination of
asyncio.StreamWriter.write()andasyncio.StreamWriter.drain().- Parameters:
data (
bytes) – data to write
- class aioftp.Throttle(*, limit: int | None = None, reset_rate: float | int = 10)¶
Throttle for streams.
- Parameters:
- class aioftp.StreamThrottle(read, write)¶
Stream throttle with read and write
aioftp.Throttle- Parameters:
read (
aioftp.Throttle) – stream read throttlewrite (
aioftp.Throttle) – stream write throttle
- clone() StreamThrottle¶
Clone throttles without memory
- classmethod from_limits(read_speed_limit: int | None = None, write_speed_limit: int | None = None) StreamThrottle¶
Simple wrapper for creation
aioftp.StreamThrottle
- class aioftp.ThrottleStreamIO(reader: StreamReader, writer: StreamWriter, throttles: dict[str, StreamThrottle] = {}, *, timeout: float | int | None = None, read_timeout: float | int | None = None, write_timeout: float | int | None = None)¶
Bases:
StreamIOThrottled
aioftp.StreamIO. ThrottleStreamIO is subclass ofaioftp.StreamIO. throttles attribute is dictionary of name:aioftp.StreamThrottlepairs- Parameters:
*args –
positional arguments for
aioftp.StreamIO**kwargs –
keyword arguments for
aioftp.StreamIOthrottles (
dictwithaioftp.Throttlevalues) – dictionary of throttles
>>> self.stream = ThrottleStreamIO( ... reader, ... writer, ... throttles={ ... "main": StreamThrottle( ... read=Throttle(...), ... write=Throttle(...) ... ) ... }, ... timeout=timeout ... )
- iter_by_block(count: int = 8192) AsyncStreamIterator¶
Read/iterate stream by block.
- Return type:
aioftp.AsyncStreamIterator
>>> async for block in stream.iter_by_block(block_size): ... ...
- iter_by_line() AsyncStreamIterator¶
Read/iterate stream by line.
- Return type:
aioftp.AsyncStreamIterator
>>> async for line in stream.iter_by_line(): ... ...
- async read(count: int = -1) bytes¶
asyncio.coroutine()aioftp.StreamIO.read()proxy
- async wait(name: str) None¶
asyncio.coroutine()Wait for all throttles
- Parameters:
name (
str) – name of throttle to acquire (“read” or “write”)
- async write(data: bytes) None¶
asyncio.coroutine()aioftp.StreamIO.write()proxy
- class aioftp.AsyncListerMixin¶
Add ability to async for context to collect data to list via await.
>>> class Context(AsyncListerMixin): ... ... >>> results = await Context(...)
- class aioftp.AbstractAsyncLister(*, timeout: float | int | None = None)¶
Abstract context with ability to collect all iterables into
listvia await with optional timeout (viaaioftp.with_timeout())>>> class Lister(AbstractAsyncLister): ... ... @with_timeout ... async def __anext__(self): ... ...
>>> async for block in Lister(...): ... ...
>>> result = await Lister(...) >>> result [block, block, block, ...]
- aioftp.with_timeout(name: str) Callable[[Callable[[WithTimeOutParamSpec], Awaitable[WithTimeOutReturnType]]], Callable[[WithTimeOutParamSpec], Awaitable[WithTimeOutReturnType]]]¶
- aioftp.with_timeout(name: Callable[[WithTimeOutParamSpec], Awaitable[WithTimeOutReturnType]]) Callable[[WithTimeOutParamSpec], Awaitable[WithTimeOutReturnType]]
Method decorator, wraps method with
asyncio.wait_for(). timeout argument takes from name decorator argument or “timeout”.- Parameters:
name (
str) – name of timeout attribute- Raises:
asyncio.TimeoutError – if coroutine does not finished in timeout
Wait for self.timeout
>>> def __init__(self, ...): ... ... self.timeout = 1 ... ... @with_timeout ... async def foo(self, ...): ... ... pass
Wait for custom timeout
>>> def __init__(self, ...): ... ... self.foo_timeout = 1 ... ... @with_timeout("foo_timeout") ... async def foo(self, ...): ... ... pass
- aioftp.async_enterable(f: Callable[[AsyncEnterableParamSpec], Awaitable[AsyncEnterableReturnType]]) Callable[[AsyncEnterableParamSpec], AsyncEnterableInstanceProtocol[AsyncEnterableReturnType]]¶
Decorator. Bring coroutine result up, so it can be used as async context
>>> async def foo(): ... ... ... ... return AsyncContextInstance(...) ... ... ctx = await foo() ... async with ctx: ... ... # do
>>> @async_enterable ... async def foo(): ... ... ... ... return AsyncContextInstance(...) ... ... async with foo() as ctx: ... ... # do ... ... ctx = await foo() ... async with ctx: ... ... # do