| 1 |
.. developer_tutorial: |
| 2 |
|
| 3 |
Developer tutorial |
| 4 |
================== |
| 5 |
|
| 6 |
Both, client and server classes are inherit-minded when created. So, you need |
| 7 |
to inherit class and override and/or add methods to bring your functionality. |
| 8 |
|
| 9 |
Client |
| 10 |
------ |
| 11 |
|
| 12 |
For simple commands, which requires no extra connection, realization of new |
| 13 |
method is pretty simple. You just need to use :py:meth:`aioftp.Client.command` |
| 14 |
(or even don't use it). For example, lets realize «NOOP» command, which do |
| 15 |
nothing: |
| 16 |
|
| 17 |
:: |
| 18 |
|
| 19 |
class MyClient(aioftp.Client): |
| 20 |
async def noop(self): |
| 21 |
await self.command("NOOP", "2xx") |
| 22 |
|
| 23 |
Lets take a look to a more complex example. Say, we want to collect some data |
| 24 |
via extra connection. For this one you need one of «extra connection» methods: |
| 25 |
:py:meth:`aioftp.Client.download_stream`, |
| 26 |
:py:meth:`aioftp.Client.upload_stream`, :py:meth:`aioftp.Client.append_stream` |
| 27 |
or (for more complex situations) :py:meth:`aioftp.Client.get_stream` |
| 28 |
Here we implements some «COLL x» command. I don't know why, but it |
| 29 |
retrieve some data via extra connection. And the size of data is equal to «x». |
| 30 |
|
| 31 |
:: |
| 32 |
|
| 33 |
class MyClient(aioftp.Client): |
| 34 |
|
| 35 |
async def collect(self, count): |
| 36 |
collected = [] |
| 37 |
async with self.get_stream("COLL " + str(count), "1xx") as stream: |
| 38 |
async for block in stream.iter_by_block(8): |
| 39 |
i = int.from_bytes(block, "big") |
| 40 |
print("received:", block, i) |
| 41 |
collected.append(i) |
| 42 |
return collected |
| 43 |
|
| 44 |
Client retrieve passive (or active in future versions) via `get_stream` and |
| 45 |
read blocks of data until connection is closed. Then finishing stream and |
| 46 |
return result. Most of client functions (except low-level from BaseClient) |
| 47 |
are made in pretty same manner. It is a good idea you to see source code of |
| 48 |
:py:class:`aioftp.Client` in client.py to see when and why this or that |
| 49 |
techniques used. |
| 50 |
|
| 51 |
Server |
| 52 |
------ |
| 53 |
|
| 54 |
Server class based on dispatcher, which wait for result of tasks via |
| 55 |
:py:meth:`asyncio.wait`. Tasks are different: command-reader, result-writer, |
| 56 |
commander-action, extra-connection-workers. FTP methods dispatched by name. |
| 57 |
|
| 58 |
Lets say we want implement «NOOP» command for server again: |
| 59 |
|
| 60 |
:: |
| 61 |
|
| 62 |
class MyServer(aioftp.Server): |
| 63 |
|
| 64 |
async def noop(self, connection, rest): |
| 65 |
connection.response("200", "boring") |
| 66 |
return True |
| 67 |
|
| 68 |
What we have here? Dispatcher calls our method with some arguments: |
| 69 |
|
| 70 |
* `connection` is state of connection, this can hold and wait for futures. |
| 71 |
There many connection values you can interest in: addresses, throttles, |
| 72 |
timeouts, extra_workers, response, etc. You can add your own flags and values |
| 73 |
to the «connection» and edit the existing ones of course. It's better to see |
| 74 |
source code of server, cause connection is heart of dispatcher ↔ task and |
| 75 |
task ↔ task interaction and state container. |
| 76 |
* `rest`: rest part of command string |
| 77 |
|
| 78 |
There is some decorators, which can help for routine checks: is user logged, |
| 79 |
can he read/write this path, etc. |
| 80 |
:py:class:`aioftp.ConnectionConditions` |
| 81 |
:py:class:`aioftp.PathConditions` |
| 82 |
:py:class:`aioftp.PathPermissions` |
| 83 |
|
| 84 |
For more complex example lets try same client «COLL x» command. |
| 85 |
|
| 86 |
:: |
| 87 |
|
| 88 |
class MyServer(aioftp.Server): |
| 89 |
|
| 90 |
@aioftp.ConnectionConditions( |
| 91 |
aioftp.ConnectionConditions.login_required, |
| 92 |
aioftp.ConnectionConditions.passive_server_started) |
| 93 |
async def coll(self, connection, rest): |
| 94 |
|
| 95 |
@aioftp.ConnectionConditions( |
| 96 |
aioftp.ConnectionConditions.data_connection_made, |
| 97 |
wait=True, |
| 98 |
fail_code="425", |
| 99 |
fail_info="Can't open data connection") |
| 100 |
@aioftp.server.worker |
| 101 |
async def coll_worker(self, connection, rest): |
| 102 |
stream = connection.data_connection |
| 103 |
del connection.data_connection |
| 104 |
async with stream: |
| 105 |
for i in range(count): |
| 106 |
binary = i.to_bytes(8, "big") |
| 107 |
await stream.write(binary) |
| 108 |
connection.response("200", "coll transfer done") |
| 109 |
return True |
| 110 |
|
| 111 |
count = int(rest) |
| 112 |
coro = coll_worker(self, connection, rest) |
| 113 |
task = connection.loop.create_task(coro) |
| 114 |
connection.extra_workers.add(task) |
| 115 |
connection.response("150", "coll transfer started") |
| 116 |
return True |
| 117 |
|
| 118 |
This action requires passive connection, that is why we use worker. We |
| 119 |
should be able to receive commands when receiving data with extra connection, |
| 120 |
that is why we send our task to dispatcher via `extra_workers`. Task will be |
| 121 |
pending on next «iteration» of dispatcher. |
| 122 |
|
| 123 |
Lets see what we have. |
| 124 |
|
| 125 |
:: |
| 126 |
|
| 127 |
async def test(): |
| 128 |
server = MyServer() |
| 129 |
client = MyClient() |
| 130 |
await server.start("127.0.0.1", 8021) |
| 131 |
await client.connect("127.0.0.1", 8021) |
| 132 |
await client.login() |
| 133 |
collected = await client.collect(20) |
| 134 |
print(collected) |
| 135 |
await client.quit() |
| 136 |
await server.close() |
| 137 |
|
| 138 |
|
| 139 |
if __name__ == "__main__": |
| 140 |
logging.basicConfig( |
| 141 |
level=logging.INFO, |
| 142 |
format="%(asctime)s [%(name)s] %(message)s", |
| 143 |
datefmt="[%H:%M:%S]:", |
| 144 |
) |
| 145 |
loop = asyncio.get_event_loop() |
| 146 |
loop.run_until_complete(test()) |
| 147 |
print("done") |
| 148 |
|
| 149 |
|
| 150 |
And the output for this is: |
| 151 |
|
| 152 |
:: |
| 153 |
|
| 154 |
[01:18:54]: [aioftp.server] serving on 127.0.0.1:8021 |
| 155 |
[01:18:54]: [aioftp.server] new connection from 127.0.0.1:48883 |
| 156 |
[01:18:54]: [aioftp.server] 220 welcome |
| 157 |
[01:18:54]: [aioftp.client] 220 welcome |
| 158 |
[01:18:54]: [aioftp.client] USER anonymous |
| 159 |
[01:18:54]: [aioftp.server] USER anonymous |
| 160 |
[01:18:54]: [aioftp.server] 230 anonymous login |
| 161 |
[01:18:54]: [aioftp.client] 230 anonymous login |
| 162 |
[01:18:54]: [aioftp.client] TYPE I |
| 163 |
[01:18:54]: [aioftp.server] TYPE I |
| 164 |
[01:18:54]: [aioftp.server] 200 |
| 165 |
[01:18:54]: [aioftp.client] 200 |
| 166 |
[01:18:54]: [aioftp.client] PASV |
| 167 |
[01:18:54]: [aioftp.server] PASV |
| 168 |
[01:18:54]: [aioftp.server] 227-listen socket created |
| 169 |
[01:18:54]: [aioftp.server] 227 (127,0,0,1,223,249) |
| 170 |
[01:18:54]: [aioftp.client] 227-listen socket created |
| 171 |
[01:18:54]: [aioftp.client] 227 (127,0,0,1,223,249) |
| 172 |
[01:18:54]: [aioftp.client] COLL 20 |
| 173 |
[01:18:54]: [aioftp.server] COLL 20 |
| 174 |
[01:18:54]: [aioftp.server] 150 coll transfer started |
| 175 |
[01:18:54]: [aioftp.client] 150 coll transfer started |
| 176 |
received: b'\x00\x00\x00\x00\x00\x00\x00\x00' 0 |
| 177 |
received: b'\x00\x00\x00\x00\x00\x00\x00\x01' 1 |
| 178 |
received: b'\x00\x00\x00\x00\x00\x00\x00\x02' 2 |
| 179 |
received: b'\x00\x00\x00\x00\x00\x00\x00\x03' 3 |
| 180 |
received: b'\x00\x00\x00\x00\x00\x00\x00\x04' 4 |
| 181 |
received: b'\x00\x00\x00\x00\x00\x00\x00\x05' 5 |
| 182 |
received: b'\x00\x00\x00\x00\x00\x00\x00\x06' 6 |
| 183 |
received: b'\x00\x00\x00\x00\x00\x00\x00\x07' 7 |
| 184 |
received: b'\x00\x00\x00\x00\x00\x00\x00\x08' 8 |
| 185 |
received: b'\x00\x00\x00\x00\x00\x00\x00\t' 9 |
| 186 |
received: b'\x00\x00\x00\x00\x00\x00\x00\n' 10 |
| 187 |
received: b'\x00\x00\x00\x00\x00\x00\x00\x0b' 11 |
| 188 |
received: b'\x00\x00\x00\x00\x00\x00\x00\x0c' 12 |
| 189 |
received: b'\x00\x00\x00\x00\x00\x00\x00\r' 13 |
| 190 |
received: b'\x00\x00\x00\x00\x00\x00\x00\x0e' 14 |
| 191 |
received: b'\x00\x00\x00\x00\x00\x00\x00\x0f' 15 |
| 192 |
received: b'\x00\x00\x00\x00\x00\x00\x00\x10' 16 |
| 193 |
received: b'\x00\x00\x00\x00\x00\x00\x00\x11' 17 |
| 194 |
received: b'\x00\x00\x00\x00\x00\x00\x00\x12' 18 |
| 195 |
[01:18:54]: [aioftp.server] 200 coll transfer done |
| 196 |
received: b'\x00\x00\x00\x00\x00\x00\x00\x13' 19 |
| 197 |
[01:18:54]: [aioftp.client] 200 coll transfer done |
| 198 |
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] |
| 199 |
[01:18:54]: [aioftp.client] QUIT |
| 200 |
[01:18:54]: [aioftp.server] QUIT |
| 201 |
[01:18:54]: [aioftp.server] 221 bye |
| 202 |
[01:18:54]: [aioftp.server] closing connection from 127.0.0.1:48883 |
| 203 |
[01:18:54]: [aioftp.client] 221 bye |
| 204 |
done |
| 205 |
|
| 206 |
It is a good idea you to see source code of :py:class:`aioftp.Server` in |
| 207 |
server.py to see when and why this or that techniques used. |
| 208 |
|
| 209 |
Path abstraction layer |
| 210 |
---------------------- |
| 211 |
|
| 212 |
Since file io is blocking and aioftp tries to be non-blocking ftp library, we |
| 213 |
need some abstraction layer for filesystem operations. That is why pathio |
| 214 |
exists. If you want to create your own pathio, then you should inherit |
| 215 |
:py:class:`aioftp.AbstractPathIO` and override it methods. |
| 216 |
|
| 217 |
User Manager |
| 218 |
------------ |
| 219 |
|
| 220 |
User manager purpose is to split retrieving user information from network or |
| 221 |
database and server logic. You can create your own user manager by inherit |
| 222 |
:py:class:`aioftp.AbstractUserManager` and override it methods. The new user |
| 223 |
manager should be passed to server as `users` argument when initialize server. |