Server API¶
- class aioftp.Server(users=None, *, loop=None, block_size=8192, socket_timeout=None, idle_timeout=None, wait_future_timeout=1, path_timeout=None, path_io_factory=<class 'aioftp.pathio.PathIO'>, maximum_connections=None, read_speed_limit=None, write_speed_limit=None, read_speed_limit_per_connection=None, write_speed_limit_per_connection=None, data_ports=None, encoding='utf-8', ssl=None)¶
Bases:
AbstractServer
FTP server.
- Parameters:
users (
tuple
orlist
ofaioftp.User
or instance ofaioftp.AbstractUserManager
subclass) – list of users or user manager objectloop (
asyncio.BaseEventLoop
) – loop to use for creating connection and binding with streamsblock_size (
int
) – bytes count for socket read operationssocket_timeout (
float
,int
orNone
) – timeout for socket read and write operationsidle_timeout (
float
,int
orNone
) – timeout for socket read operations, another words: how long user can keep silence without sending commandswait_future_timeout (
float
,int
orNone
) – wait for data connection to establishpath_timeout (
float
,int
orNone
) – timeout for path-related operations (make directory, unlink file, etc.)path_io_factory (
aioftp.AbstractPathIO
) – factory of «path abstract layer»maximum_connections (
int
) – Maximum command connections per serverread_speed_limit (
int
orNone
) – server read speed limit in bytes per secondwrite_speed_limit (
int
orNone
) – server write speed limit in bytes per secondread_speed_limit_per_connection (
int
orNone
) – server read speed limit per connection in bytes per secondwrite_speed_limit_per_connection (
int
orNone
) – server write speed limit per connection in bytes per seconddata_ports (
collections.Iterable
orNone
) – port numbers that are available for passive connectionsencoding (
str
) – encoding to use for convertion strings to bytesssl (
ssl.SSLContext
) – can be set to anssl.SSLContext
instance to enable TLS over the accepted connections. Please lookasyncio.loop.create_server()
docs.
- async close()¶
asyncio.coroutine()
Shutdown the server and close all connections.
- async start(host=None, port=0, **kwargs)¶
asyncio.coroutine()
Start server.
- Parameters:
host (
str
) – ip address to bind for listening.port (
int
) – port number to bind for listening.kwargs – keyword arguments, they passed to
asyncio.start_server()
- aioftp.server.worker(f)¶
Decorator. Abortable worker. If wrapped task will be cancelled by dispatcher, decorator will send ftp codes of successful interrupt.
>>> @worker ... async def worker(self, connection, rest): ... ...
- class aioftp.User(login=None, password=None, *, base_path=PosixPath('.'), home_path=PurePosixPath('/'), permissions=None, maximum_connections=None, read_speed_limit=None, write_speed_limit=None, read_speed_limit_per_connection=None, write_speed_limit_per_connection=None)¶
User description.
- Parameters:
login (
str
) – user loginpassword (
str
) – user passwordbase_path (
str
orpathlib.Path
) – real user path for file io operationshome_path (
str
orpathlib.PurePosixPath
) – virtual user path for client representation (must be absolute)permissions (
tuple
orlist
ofaioftp.Permission
) – list of path permissionsmaximum_connections (
int
) – Maximum connections per userread_speed_limit (
int
orNone
) – read speed limit per user in bytes per secondwrite_speed_limit (
int
orNone
) – write speed limit per user in bytes per secondread_speed_limit_per_connection (
int
orNone
) – read speed limit per user connection in bytes per secondwrite_speed_limit_per_connection (
int
orNone
) – write speed limit per user connection in bytes per second
- get_permissions(path)¶
Return nearest parent permission for path.
- Parameters:
path (
str
orpathlib.PurePosixPath
) – path which permission you want to know- Return type:
- class aioftp.Permission(path='/', *, readable=True, writable=True)¶
Path permission
- Parameters:
path (
str
orpathlib.PurePosixPath
) – pathreadable (
bool
) – is readablewritable (
bool
) – is writable
- class aioftp.AbstractUserManager(*, timeout=None, loop=None)¶
Abstract user manager.
- Parameters:
- async authenticate(user, password)¶
asyncio.coroutine()
Check if user can be authenticated with provided password
- Parameters:
user (
aioftp.User
) – userpassword (
str
) – password
- Return type:
- async get_user(login)¶
asyncio.coroutine()
Get user and response for USER call
- Parameters:
login (
str
) – user’s login
- async notify_logout(user)¶
asyncio.coroutine()
Called when user connection is closed if user was initiated
- Parameters:
user (
aioftp.User
) – user
- class aioftp.server.MemoryUserManager(users, *args, **kwargs)¶
A built-in user manager that keeps predefined set of users in memory.
- Parameters:
users (
list
,tuple
, etc. ofaioftp.User
) – container of users
- async authenticate(user, password)¶
asyncio.coroutine()
Check if user can be authenticated with provided password
- Parameters:
user (
aioftp.User
) – userpassword (
str
) – password
- Return type:
- async get_user(login)¶
asyncio.coroutine()
Get user and response for USER call
- Parameters:
login (
str
) – user’s login
- async notify_logout(user)¶
asyncio.coroutine()
Called when user connection is closed if user was initiated
- Parameters:
user (
aioftp.User
) – user
- class aioftp.Connection(*, loop=None, **kwargs)¶
Bases:
defaultdict
Connection state container for transparent work with futures for async wait
- Parameters:
loop (
asyncio.BaseEventLoop
) – event loopkwargs – initialization parameters
Container based on
collections.defaultdict
, which holdsasyncio.Future
as default factory. There is two layers of abstraction:- Low level based on simple dictionary keys to attributes mapping and
available at Connection.future.
- High level based on futures result and dictionary keys to attributes
mapping and available at Connection.
To clarify, here is groups of equal expressions
>>> connection.future.foo >>> connection["foo"] >>> connection.foo >>> connection["foo"].result() >>> del connection.future.foo >>> del connection.foo >>> del connection["foo"]
- class aioftp.AvailableConnections(value=None)¶
Semaphore-like object. Have no blocks, only raises ValueError on bounds crossing. If value is
None
have no limits (bounds checks).- Parameters:
value (
int
orNone
)
- acquire()¶
Acquire, decrementing the internal counter by one.
- release()¶
Release, incrementing the internal counter by one.
- class aioftp.ConnectionConditions(*fields, wait=False, fail_code='503', fail_info=None)¶
Decorator for checking connection keys for existence or wait for them. Available options:
- Parameters:
fields –
ConnectionConditions.user_required — required “user” key, user already identified
ConnectionConditions.login_required — required “logged” key, user already logged in.
ConnectionConditions.passive_server_started — required “passive_server” key, user already send PASV and server awaits incomming connection
ConnectionConditions.data_connection_made — required “data_connection” key, user already connected to passive connection
ConnectionConditions.rename_from_required — required “rename_from” key, user already tell filename for rename
wait (
bool
) – Indicates if should wait for parameters for connection.wait_future_timeoutfail_code (
str
) – return code if failurefail_info (
str
) – return information string if failure. IfNone
, then use default string
>>> @ConnectionConditions( ... ConnectionConditions.login_required, ... ConnectionConditions.passive_server_started, ... ConnectionConditions.data_connection_made, ... wait=True) ... def foo(self, connection, rest): ... ...
- class aioftp.PathConditions(*conditions)¶
Decorator for checking paths. Available options:
path_must_exists
path_must_not_exists
path_must_be_dir
path_must_be_file
>>> @PathConditions( ... PathConditions.path_must_exists, ... PathConditions.path_must_be_dir) ... def foo(self, connection, path): ... ...
- class aioftp.PathPermissions(*permissions)¶
Decorator for checking path permissions. There is two permissions right now:
PathPermissions.readable
PathPermissions.writable
Decorator will check the permissions and return proper code and information to client if permission denied
>>> @PathPermissions( ... PathPermissions.readable, ... PathPermissions.writable) ... def foo(self, connection, path): ... ...