| 1 |
.. server_tutorial: |
| 2 |
|
| 3 |
Server tutorial |
| 4 |
=============== |
| 5 |
|
| 6 |
aioftp server is much more like a tool. You configure it, run and forget about |
| 7 |
it. |
| 8 |
|
| 9 |
Configuring server |
| 10 |
------------------ |
| 11 |
|
| 12 |
At first you should create :class:`aioftp.Server` instance and start it |
| 13 |
|
| 14 |
:py:meth:`aioftp.Server.start` |
| 15 |
|
| 16 |
:: |
| 17 |
|
| 18 |
>>> server = aioftp.Server() |
| 19 |
>>> await server.start() |
| 20 |
|
| 21 |
Default arguments allow anonymous login and read/write current directory. So, |
| 22 |
there is one user with anonymous login and read/write permissions on "/" |
| 23 |
virtual path. Real path is current working directory. |
| 24 |
|
| 25 |
Dealing with users and permissions |
| 26 |
---------------------------------- |
| 27 |
|
| 28 |
You can specify as much users as you want, just pass list of them when creating |
| 29 |
:class:`aioftp.Server` instance |
| 30 |
|
| 31 |
:class:`aioftp.User` |
| 32 |
|
| 33 |
:class:`aioftp.Permission` |
| 34 |
|
| 35 |
:: |
| 36 |
|
| 37 |
>>> users = ( |
| 38 |
... aioftp.User( |
| 39 |
... "Guido", |
| 40 |
... "secret_password", |
| 41 |
... home_path="/Guido", |
| 42 |
... permissions=( |
| 43 |
... aioftp.Permission("/", readable=False, writable=False), |
| 44 |
... aioftp.Permission("/Guido", readable=True, writable=True), |
| 45 |
... ) |
| 46 |
... ), |
| 47 |
... aioftp.User( |
| 48 |
... home_path="/anon", |
| 49 |
... permissions=( |
| 50 |
... aioftp.Permission("/", readable=False, writable=False), |
| 51 |
... aioftp.Permission("/anon", readable=True), |
| 52 |
... ) |
| 53 |
... ), |
| 54 |
... ) |
| 55 |
>>> server = aioftp.Server(users) |
| 56 |
>>> await server.start() |
| 57 |
|
| 58 |
This will create two users: "Guido", who can read and write to "/Guido" folder, |
| 59 |
which is home folder, but can't read/write the root and other directories and |
| 60 |
anonymous user, who home directory is "/anon" and there is only read |
| 61 |
permission. |
| 62 |
|
| 63 |
Path abstraction layer |
| 64 |
---------------------- |
| 65 |
|
| 66 |
aioftp provides abstraction of file system operations. You can use exist ones: |
| 67 |
|
| 68 |
* :py:class:`aioftp.PathIO` — blocking path operations |
| 69 |
* :py:class:`aioftp.AsyncPathIO` — non-blocking path operations, this one is |
| 70 |
blocking ones just wrapped with |
| 71 |
:py:meth:`asyncio.BaseEventLoop.run_in_executor`. It's really slow, so it's |
| 72 |
better to avoid usage of this path io layer. |
| 73 |
* :py:class:`aioftp.MemoryPathIO` — in-memory realization of file system, this |
| 74 |
one is just proof of concept and probably not too fast (as it can be). |
| 75 |
|
| 76 |
You can specify `path_io_factory` when creating :py:class:`aioftp.Server` |
| 77 |
instance. Default factory is :py:class:`aioftp.PathIO`. |
| 78 |
|
| 79 |
:: |
| 80 |
|
| 81 |
>>> server = aioftp.Server(path_io_factory=aioftp.MemoryPathIO) |
| 82 |
>>> await server.start() |
| 83 |
|
| 84 |
Dealing with timeouts |
| 85 |
--------------------- |
| 86 |
|
| 87 |
There is three different timeouts you can specify: |
| 88 |
|
| 89 |
* `socket_timeout` — timeout for low-level socket operations |
| 90 |
:py:meth:`asyncio.StreamReader.read`, |
| 91 |
:py:meth:`asyncio.StreamReader.readline` and |
| 92 |
:py:meth:`asyncio.StreamWriter.drain`. This one does not affects awaiting |
| 93 |
command read operation. |
| 94 |
* `path_timeout` — timeout for file system operations |
| 95 |
* `idle_timeout` — timeout for socket read operation when awaiting command, |
| 96 |
another words: how long user can keep silence without sending commands |
| 97 |
* `wait_future_timeout` — timeout for waiting connection states (the main |
| 98 |
purpose is wait for passive connection) |
| 99 |
|
| 100 |
Maximum connections |
| 101 |
------------------- |
| 102 |
|
| 103 |
Connections count can be specified: |
| 104 |
|
| 105 |
* per server |
| 106 |
* per user |
| 107 |
|
| 108 |
First one via server constructor |
| 109 |
|
| 110 |
:: |
| 111 |
|
| 112 |
>>> server = aioftp.Server(maximum_connections=3) |
| 113 |
|
| 114 |
Second one via user class |
| 115 |
|
| 116 |
:: |
| 117 |
|
| 118 |
>>> users = (aioftp.User(maximum_connections=3),) |
| 119 |
>>> server = aioftp.Server(users) |
| 120 |
|
| 121 |
Throttle |
| 122 |
-------- |
| 123 |
|
| 124 |
Server have many options for read/write speed throttle: |
| 125 |
|
| 126 |
* global per server |
| 127 |
* per connection |
| 128 |
* global per user |
| 129 |
* per user connection |
| 130 |
|
| 131 |
"Global per server" and "per connection" can be provided by constructor |
| 132 |
|
| 133 |
:: |
| 134 |
|
| 135 |
>>> server = aioftp.Server( |
| 136 |
... read_speed_limit=1024 * 1024, |
| 137 |
... write_speed_limit=1024 * 1024, |
| 138 |
... read_speed_limit_per_connection=100 * 1024, |
| 139 |
... write_speed_limit_per_connection=100 * 1024 |
| 140 |
... ) |
| 141 |
|
| 142 |
User throttles can be provided by user constructor |
| 143 |
|
| 144 |
:: |
| 145 |
|
| 146 |
>>> users = ( |
| 147 |
... aioftp.User( |
| 148 |
... read_speed_limit=1024 * 1024, |
| 149 |
... write_speed_limit=1024 * 1024, |
| 150 |
... read_speed_limit_per_connection=100 * 1024, |
| 151 |
... write_speed_limit_per_connection=100 * 1024 |
| 152 |
... ), |
| 153 |
... ) |
| 154 |
>>> server = aioftp.Server(users) |
| 155 |
|
| 156 |
Stopping the server |
| 157 |
------------------- |
| 158 |
|
| 159 |
:: |
| 160 |
|
| 161 |
>>> await server.close() |
| 162 |
|
| 163 |
WARNING |
| 164 |
------- |
| 165 |
|
| 166 |
:py:meth:`aioftp.Server.list` use :py:meth:`aioftp.Server.build_list_string`, |
| 167 |
which should produce `LIST` strings with :py:meth:`datetime.datetime.strftime`. |
| 168 |
For proper work (in part of formatting month abbreviation) locale should be |
| 169 |
setted to "C". For this reason if you use multithreaded app, and use some |
| 170 |
locale-dependent stuff, you should use :py:meth:`aioftp.setlocale` context |
| 171 |
manager when you dealing with locale in another thread. |
| 172 |
|
| 173 |
Futher reading |
| 174 |
-------------- |
| 175 |
:doc:`server_api` |