Server API¶
-
class
aioftp.
Server
(users=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, ipv4_pasv_forced_response_address=None, data_ports=None, encoding='utf-8', ssl=None)¶ Bases:
object
FTP server.
Parameters: - users (
tuple
orlist
ofaioftp.User
or instance ofaioftp.AbstractUserManager
subclass) – list of users or user manager object - block_size (
int
) – bytes count for socket read operations - socket_timeout (
float
,int
orNone
) – timeout for socket read and write operations - idle_timeout (
float
,int
orNone
) – timeout for socket read operations, another words: how long user can keep silence without sending commands - wait_future_timeout (
float
,int
orNone
) – wait for data connection to establish - path_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 server - read_speed_limit (
int
orNone
) – server read speed limit in bytes per second - write_speed_limit (
int
orNone
) – server write speed limit in bytes per second - read_speed_limit_per_connection (
int
orNone
) – server read speed limit per connection in bytes per second - write_speed_limit_per_connection (
int
orNone
) – server write speed limit per connection in bytes per second - ipv4_pasv_forced_response_address (
str
orNone
) – external IPv4 address for passive connections - data_ports (
collections.Iterable
orNone
) – port numbers that are available for passive connections - encoding (
str
) – encoding to use for convertion strings to bytes - ssl (
ssl.SSLContext
) – can be set to anssl.SSLContext
instance to enable TLS over the accepted connections. Please lookasyncio.loop.create_server()
docs.
-
close
()¶ asyncio.coroutine()
Shutdown the server and close all connections.
-
run
(host=None, port=0, **kwargs)¶ asyncio.coroutine()
Single entrypoint to start, serve and close.
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()
- host (
-
serve_forever
()¶ asyncio.coroutine()
Proxy to
asyncio.Server
serve_forever method.
-
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()
- host (
- users (
-
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 login - password (
str
) – user password - base_path (
str
orpathlib.Path
) – real user path for file io operations - home_path (
str
orpathlib.PurePosixPath
) – virtual user path for client representation (must be absolute) - permissions (
tuple
orlist
ofaioftp.Permission
) – list of path permissions - maximum_connections (
int
) – Maximum connections per user - read_speed_limit (
int
orNone
) – read speed limit per user in bytes per second - write_speed_limit (
int
orNone
) – write speed limit per user in bytes per second - read_speed_limit_per_connection (
int
orNone
) – read speed limit per user connection in bytes per second - write_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 knowReturn type: aioftp.Permission
- login (
-
class
aioftp.
Permission
(path='/', *, readable=True, writable=True)¶ Path permission
Parameters: - path (
str
orpathlib.PurePosixPath
) – path - readable (
bool
) – is readable - writable (
bool
) – is writable
- path (
-
class
aioftp.
AbstractUserManager
(*, timeout=None)¶ Abstract user manager.
Parameters: timeout ( float
,int
orNone
) – timeout used by with_timeout decorator-
authenticate
(user, password)¶ asyncio.coroutine()
Check if user can be authenticated with provided password
Parameters: - user (
aioftp.User
) – user - password (
str
) – password
Return type: - user (
-
get_user
(login)¶ asyncio.coroutine()
Get user and response for USER call
Parameters: login ( str
) – user’s login
-
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-
authenticate
(user, password)¶ asyncio.coroutine()
Check if user can be authenticated with provided password
Parameters: - user (
aioftp.User
) – user - password (
str
) – password
Return type: - user (
-
get_user
(login)¶ asyncio.coroutine()
Get user and response for USER call
Parameters: login ( str
) – user’s login
-
notify_logout
(user)¶ asyncio.coroutine()
Called when user connection is closed if user was initiated
Parameters: user ( aioftp.User
) – user
-
-
class
aioftp.
Connection
(**kwargs)¶ Bases:
collections.defaultdict
Connection state container for transparent work with futures for async wait
Parameters: kwargs – 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_timeout - fail_code (
str
) – return code if failure - fail_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): ... ...
- fields –
-
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): ... ...