File: client_tutorial.rst

package info (click to toggle)
aioftp 0.26.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 624 kB
  • sloc: python: 5,566; makefile: 172
file content (426 lines) | stat: -rw-r--r-- 12,690 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
Client tutorial
===============

In 95% cases it is enough to use a little more than 10 client coroutines. So,
lets start!

Connecting to server
--------------------

Firstly you should create :class:`aioftp.Client` instance and connect to host

:py:meth:`aioftp.Client.connect`

:py:meth:`aioftp.Client.login`

::

    >>> client = aioftp.Client()
    >>> await client.connect("ftp.server.com")
    >>> await client.login("user", "pass")

Or just use :class:`aioftp.context` async context, which will connect,
login and quit automatically

::

    >>> async with aioftp.Client.context("ftp.server.com", user="user", password="pass") as client:
    ...     # do

Download and upload paths
-------------------------

:py:meth:`aioftp.Client.upload` and :py:meth:`aioftp.Client.download`
coroutines are pretty similar, except data flow direction. You can
upload/download file or directory. There is "source" and "destination". When
you does not specify "destination", then current working directory will be
used as destination.

Lets upload some file to current directory
::

    >>> await client.upload("test.py")

If you want specify new name, or different path to uploading/downloading path
you should use "write_into" argument, which works for directory as well
::

    >>> await client.upload("test.py", "tmp/test.py", write_into=True)
    >>> await client.upload("folder1", "folder2", write_into=True)

After that you get
::

    tmp/test.py
    folder2/*content of folder1*

If you will not use "write_into", you will get something you probably did not
expect
::

    tmp/test.py/test.py
    folder2/folder1/*content of folder1*

Or you can upload path as is and then rename it
(:py:meth:`aioftp.Client.rename`)

Downloading is pretty same
::

    >>> await client.download("tmp/test.py", "foo.py", write_into=True)
    >>> await client.download("folder2")

Listing paths
-------------

For listing paths you should use :py:meth:`aioftp.Client.list` coroutine, which
can list paths recursively and produce a :py:class:`list` and can be used
with `async for`

::

    >>> await client.list("/")
    [(PosixPath('/.logs'), {'unix.mode': '0755', 'unique': '801g4804045', ...

::

    >>> await client.list("/", recursive=True)
    [(PosixPath('/.logs'), {'unix.mode': '0755', 'unique': '801g4804045', ...

::

    >>> async for path, info in client.list("/", recursive=True):
    ...     print(path)
    (PosixPath('/.logs'), {'unix.mode': '0755', 'unique': '801g4804045', ...

If you ommit path argument, result will be list for current working directory

::

    >>> await c.list()
    [(PosixPath('test.py'), {'unique': '801g480a508', 'size': '3102', ...

In case of `async for` be careful, since asynchronous variation of list is lazy.
It means that **you can't interact with server until you leave `async for` block.**
If you need list and interact with server you should use eager version of list:

::

    >>> for path, info in (await client.list()):
    ...     await client.download(path, path.name)

If you want to mix lazy `list` and client interaction, you can create two client
connections to server:

::

    >>> async for path, info in client1.list():
    ...     await client2.download(path, path.name)

WARNING
^^^^^^^

:py:meth:`aioftp.Client.list` in general use `MLSD` command, but some nasty
servers does not support this command. Then client will try to use `LIST`
command, and parse server response. For proper work of
:py:meth:`datetime.datetime.strptime` (in part of parsing month abbreviation)
locale should be setted to "C". For this reason if you use multithreaded app,
and use some locale-dependent stuff, you should use
:py:meth:`aioftp.setlocale` context manager when you dealing with locale in
another thread.
**since 0.8.1**
If fallback `LIST` parser can't parse line, then this line will be ignored, so
fallback `LIST` implementation will never raise exception.

Getting path stats
------------------

When you need get some path stats you should use :py:meth:`aioftp.Client.stat`

::

    >>> await client.stat("tmp2.py")
    {'size': '909', 'create': '1445437246.4320722', 'type': 'file', ...
    >>> await client.stat(".git")
    {'create': '1445435702.6441028', 'type': 'dir', 'size': '4096', ...

If you need just to check path for is it file, directory or exists you can use

    :py:meth:`aioftp.Client.is_file`

    :py:meth:`aioftp.Client.is_dir`

    :py:meth:`aioftp.Client.exists`

::

    >>> await client.is_file("/public_html")
    False
    >>> await client.is_dir("/public_html")
    True
    >>> await client.is_file("test.py")
    True
    >>> await client.exists("test.py")
    True
    >>> await client.exists("naked-guido.png")
    False

WARNING
^^^^^^^

:py:meth:`aioftp.Client.stat` in general use `MLST` command, but some nasty
servers does not support this command. Then client will try to use `LIST`
command, and parse server response. For proper work of
:py:meth:`datetime.datetime.strptime` (in part of parsing month abbreviation)
locale should be setted to "C". For this reason if you use multithreaded app,
and use some locale-dependent stuff, you should use
:py:meth:`aioftp.setlocale` context manager when you dealing with locale in
another thread.
**since 0.8.1**
If fallback `LIST` parser can't parse line, then this line will be ignored, so
fallback `LIST` implementation will never raise exception. But if requested
path line can't be parsed, then :py:meth:`aioftp.Client.stat` method will
raise `path does not exists`.


Remove path
-----------

For removing paths you have universal coroutine :py:meth:`aioftp.Client.remove`
which can remove file or directory recursive. So, you don't need to do borring
checks.

::

    >>> await client.remove("tmp.py")
    >>> await client.remove("folder1")

Dealing with directories
------------------------

Directories coroutines are pretty simple.

:py:meth:`aioftp.Client.get_current_directory`

:py:meth:`aioftp.Client.change_directory`

:py:meth:`aioftp.Client.make_directory`

::

    >>> await client.get_current_directory()
    PosixPath('/public_html')
    >>> await client.change_directory("folder1")
    >>> await client.get_current_directory()
    PosixPath('/public_html/folder1')
    >>> await client.change_directory()
    >>> await client.get_current_directory()
    PosixPath('/public_html')
    >>> await client.make_directory("folder2")
    >>> await client.change_directory("folder2")
    >>> await client.get_current_directory()
    PosixPath('/public_html/folder2')

Rename (move) path
------------------

To change name (move) file or directory use :py:meth:`aioftp.Client.rename`.

::

    >>> await client.list()
    [(PosixPath('test.py'), {'modify': '20150423090041', 'type': 'file', ...
    >>> await client.rename("test.py", "foo.py")
    >>> await client.list()
    [(PosixPath('foo.py'), {'modify': '20150423090041', 'type': 'file', ...

Closing connection
------------------

:py:meth:`aioftp.Client.quit` coroutine will send "QUIT" ftp command and close
connection.

::

    >>> await client.quit()

Advanced download and upload, abort, restart
--------------------------------------------

File read/write operations are blocking and slow. So if you want just
parse/calculate something on the fly when receiving file, or generate data
to upload it to file system on ftp server, then you should use
:py:meth:`aioftp.Client.download_stream`,
:py:meth:`aioftp.Client.upload_stream` and
:py:meth:`aioftp.Client.append_stream`. All this methods based on
:py:meth:`aioftp.Client.get_stream`, which return
:py:class:`aioftp.DataConnectionThrottleStreamIO`. The common pattern to
work with streams is:

::

    >>> async with client.download_stream("tmp.py") as stream:
    ...     async for block in stream.iter_by_block():
    ...         # do something with data

Or, if you want to abort transfer at some point

::

    >>> stream = await client.download_stream("tmp.py")
    ... async for block in stream.iter_by_block():
    ...     # do something with data
    ...     if something_not_interesting:
    ...         await client.abort()
    ...         stream.close()
    ...         break
    ... else:
    ...     await stream.finish()

WARNING
^^^^^^^

Do not use `async with <stream>` syntax if you want to use `abort`, this will
lead to deadlock.

For restarting upload/download at exact byte position (REST command) there is
`offset` argument for `*_stream` methods:

::

    >>> async with client.download_stream("tmp.py", offset=256) as stream:
    ...     async for block in stream.iter_by_block():
    ...         # do something with data

Or if you want to restore upload/download process:

::

    >>> while True:
    ...     try:
    ...         async with aioftp.Client.context(HOST, PORT) as client:
    ...             if await client.exists(filename):
    ...                 stat = await client.stat(filename)
    ...                 size = int(stat["size"])
    ...             else:
    ...                 size = 0
    ...             file_in.seek(size)
    ...             async with client.upload_stream(filename, offset=size) as stream:
    ...                 while True:
    ...                     data = file_in.read(block_size)
    ...                     if not data:
    ...                         break
    ...                     await stream.write(data)
    ...         break
    ...     except ConnectionResetError:
    ...         pass

The idea is to seek position of source «file» for upload and start
upload + offset/append. Opposite situation for download («file» append and
download + offset)

Throttle
--------

Client have two types of speed limit: `read_speed_limit` and
`write_speed_limit`. Throttle can be set at initialization time:

::

    >>> client = aioftp.Client(read_speed_limit=100 * 1024)  # 100 Kib/s

And can be changed after creation:

::

    >>> client.throttle.write.limit = 250 * 1024

Path abstraction layer
----------------------

aioftp provides abstraction of file system operations. You can use exist ones:

* :py:class:`aioftp.PathIO` — blocking path operations
* :py:class:`aioftp.AsyncPathIO` — non-blocking path operations, this one is
  blocking ones just wrapped with
  :py:meth:`asyncio.BaseEventLoop.run_in_executor`. It's really slow, so it's
  better to avoid usage of this path io layer.
* :py:class:`aioftp.MemoryPathIO` — in-memory realization of file system, this
  one is just proof of concept and probably not too fast (as it can be).

You can specify `path_io_factory` when creating :py:class:`aioftp.Client`
instance. Default factory is :py:class:`aioftp.PathIO`.

::

    >>> client = aioftp.Client(path_io_factory=pathio.MemoryPathIO)

Timeouts
--------

:py:class:`aioftp.Client` have `socket_timeout` argument, which you can use
to specify global timeout for socket io operations.

::

    >>> client = aioftp.Client(socket_timeout=1)  # 1 second socket timeout

:py:class:`aioftp.Client` also have `path_timeout`, which is applied
**only for non-blocking path io layers**.

::

    >>> client = aioftp.Client(
    ...     path_timeout=1,
    ...     path_io_factory=pathio.AsyncPathIO
    ... )

TLS Upgrade Support
-------------------

Just like Python's `ftplib.FTP_TLS`, aioftp supports TLS upgrade. This is
done by calling :py:meth:`aioftp.Client.upgrade_to_tls` after instantiating the
client, like:

::

    >>> client = aioftp.Client()
    >>> await client.connect("ftp.server.com")
    >>> await client.upgrade_to_tls()
    >>> await client.login("user", "pass")

Using proxy
-----------

Simplest way to use socks proxy with :class:`aioftp.Client`
is `siosocks <https://github.com/pohmelie/siosocks>`_

::

    >>> client = aioftp.Client(
    ...     socks_host="localhost",
    ...     socks_port=9050,
    ...     socks_version=5,
    ... )

Don't forget to install `aioftp` as `pip install aioftp[socks]`, or install
`siosocks` directly with `pip install siosocks`.

WARNING
-------

:py:meth:`aioftp.Client.list` and :py:meth:`aioftp.Client.stat` in general
use `MLSD` and `MLST`, but some nasty servers does not support this commands.
Then client will try to use `LIST` command, and parse server response.
For proper work of :py:meth:`datetime.datetime.strptime` (in part of parsing
month abbreviation) locale should be setted to "C". For this reason if you use
multithreaded app, and use some locale-dependent stuff, you should use
:py:meth:`aioftp.setlocale` context manager when you dealing with locale in
another thread.
**since 0.8.1**
If fallback `LIST` parser can't parse line, then this line will be ignored, so
fallback `LIST` implementation will never raise exception.

Futher reading
--------------
:doc:`client_api`