| 1 |
Client tutorial |
| 2 |
=============== |
| 3 |
|
| 4 |
In 95% cases it is enough to use a little more than 10 client coroutines. So, |
| 5 |
lets start! |
| 6 |
|
| 7 |
Connecting to server |
| 8 |
-------------------- |
| 9 |
|
| 10 |
Firstly you should create :class:`aioftp.Client` instance and connect to host |
| 11 |
|
| 12 |
:py:meth:`aioftp.Client.connect` |
| 13 |
|
| 14 |
:py:meth:`aioftp.Client.login` |
| 15 |
|
| 16 |
:: |
| 17 |
|
| 18 |
>>> client = aioftp.Client() |
| 19 |
>>> await client.connect("ftp.server.com") |
| 20 |
>>> await client.login("user", "pass") |
| 21 |
|
| 22 |
Or just use :class:`aioftp.context` async context, which will connect, |
| 23 |
login and quit automatically |
| 24 |
|
| 25 |
:: |
| 26 |
|
| 27 |
>>> async with aioftp.Client.context("ftp.server.com", user="user", password="pass") as client: |
| 28 |
... # do |
| 29 |
|
| 30 |
Download and upload paths |
| 31 |
------------------------- |
| 32 |
|
| 33 |
:py:meth:`aioftp.Client.upload` and :py:meth:`aioftp.Client.download` |
| 34 |
coroutines are pretty similar, except data flow direction. You can |
| 35 |
upload/download file or directory. There is "source" and "destination". When |
| 36 |
you does not specify "destination", then current working directory will be |
| 37 |
used as destination. |
| 38 |
|
| 39 |
Lets upload some file to current directory |
| 40 |
:: |
| 41 |
|
| 42 |
>>> await client.upload("test.py") |
| 43 |
|
| 44 |
If you want specify new name, or different path to uploading/downloading path |
| 45 |
you should use "write_into" argument, which works for directory as well |
| 46 |
:: |
| 47 |
|
| 48 |
>>> await client.upload("test.py", "tmp/test.py", write_into=True) |
| 49 |
>>> await client.upload("folder1", "folder2", write_into=True) |
| 50 |
|
| 51 |
After that you get |
| 52 |
:: |
| 53 |
|
| 54 |
tmp/test.py |
| 55 |
folder2/*content of folder1* |
| 56 |
|
| 57 |
If you will not use "write_into", you will get something you probably did not |
| 58 |
expect |
| 59 |
:: |
| 60 |
|
| 61 |
tmp/test.py/test.py |
| 62 |
folder2/folder1/*content of folder1* |
| 63 |
|
| 64 |
Or you can upload path as is and then rename it |
| 65 |
(:py:meth:`aioftp.Client.rename`) |
| 66 |
|
| 67 |
Downloading is pretty same |
| 68 |
:: |
| 69 |
|
| 70 |
>>> await client.download("tmp/test.py", "foo.py", write_into=True) |
| 71 |
>>> await client.download("folder2") |
| 72 |
|
| 73 |
Listing paths |
| 74 |
------------- |
| 75 |
|
| 76 |
For listing paths you should use :py:meth:`aioftp.Client.list` coroutine, which |
| 77 |
can list paths recursively and produce a :py:class:`list` and can be used |
| 78 |
with `async for` |
| 79 |
|
| 80 |
:: |
| 81 |
|
| 82 |
>>> await client.list("/") |
| 83 |
[(PosixPath('/.logs'), {'unix.mode': '0755', 'unique': '801g4804045', ... |
| 84 |
|
| 85 |
:: |
| 86 |
|
| 87 |
>>> await client.list("/", recursive=True) |
| 88 |
[(PosixPath('/.logs'), {'unix.mode': '0755', 'unique': '801g4804045', ... |
| 89 |
|
| 90 |
:: |
| 91 |
|
| 92 |
>>> async for path, info in client.list("/", recursive=True): |
| 93 |
... print(path) |
| 94 |
(PosixPath('/.logs'), {'unix.mode': '0755', 'unique': '801g4804045', ... |
| 95 |
|
| 96 |
If you ommit path argument, result will be list for current working directory |
| 97 |
|
| 98 |
:: |
| 99 |
|
| 100 |
>>> await c.list() |
| 101 |
[(PosixPath('test.py'), {'unique': '801g480a508', 'size': '3102', ... |
| 102 |
|
| 103 |
In case of `async for` be careful, since asynchronous variation of list is lazy. |
| 104 |
It means that **you can't interact with server until you leave `async for` block.** |
| 105 |
If you need list and interact with server you should use eager version of list: |
| 106 |
|
| 107 |
:: |
| 108 |
|
| 109 |
>>> for path, info in (await client.list()): |
| 110 |
... await client.download(path, path.name) |
| 111 |
|
| 112 |
If you want to mix lazy `list` and client interaction, you can create two client |
| 113 |
connections to server: |
| 114 |
|
| 115 |
:: |
| 116 |
|
| 117 |
>>> async for path, info in client1.list(): |
| 118 |
... await client2.download(path, path.name) |
| 119 |
|
| 120 |
WARNING |
| 121 |
^^^^^^^ |
| 122 |
|
| 123 |
:py:meth:`aioftp.Client.list` in general use `MLSD` command, but some nasty |
| 124 |
servers does not support this command. Then client will try to use `LIST` |
| 125 |
command, and parse server response. For proper work of |
| 126 |
:py:meth:`datetime.datetime.strptime` (in part of parsing month abbreviation) |
| 127 |
locale should be setted to "C". For this reason if you use multithreaded app, |
| 128 |
and use some locale-dependent stuff, you should use |
| 129 |
:py:meth:`aioftp.setlocale` context manager when you dealing with locale in |
| 130 |
another thread. |
| 131 |
**since 0.8.1** |
| 132 |
If fallback `LIST` parser can't parse line, then this line will be ignored, so |
| 133 |
fallback `LIST` implementation will never raise exception. |
| 134 |
|
| 135 |
Getting path stats |
| 136 |
------------------ |
| 137 |
|
| 138 |
When you need get some path stats you should use :py:meth:`aioftp.Client.stat` |
| 139 |
|
| 140 |
:: |
| 141 |
|
| 142 |
>>> await client.stat("tmp2.py") |
| 143 |
{'size': '909', 'create': '1445437246.4320722', 'type': 'file', ... |
| 144 |
>>> await client.stat(".git") |
| 145 |
{'create': '1445435702.6441028', 'type': 'dir', 'size': '4096', ... |
| 146 |
|
| 147 |
If you need just to check path for is it file, directory or exists you can use |
| 148 |
|
| 149 |
:py:meth:`aioftp.Client.is_file` |
| 150 |
|
| 151 |
:py:meth:`aioftp.Client.is_dir` |
| 152 |
|
| 153 |
:py:meth:`aioftp.Client.exists` |
| 154 |
|
| 155 |
:: |
| 156 |
|
| 157 |
>>> await client.is_file("/public_html") |
| 158 |
False |
| 159 |
>>> await client.is_dir("/public_html") |
| 160 |
True |
| 161 |
>>> await client.is_file("test.py") |
| 162 |
True |
| 163 |
>>> await client.exists("test.py") |
| 164 |
True |
| 165 |
>>> await client.exists("naked-guido.png") |
| 166 |
False |
| 167 |
|
| 168 |
WARNING |
| 169 |
^^^^^^^ |
| 170 |
|
| 171 |
:py:meth:`aioftp.Client.stat` in general use `MLST` command, but some nasty |
| 172 |
servers does not support this command. Then client will try to use `LIST` |
| 173 |
command, and parse server response. For proper work of |
| 174 |
:py:meth:`datetime.datetime.strptime` (in part of parsing month abbreviation) |
| 175 |
locale should be setted to "C". For this reason if you use multithreaded app, |
| 176 |
and use some locale-dependent stuff, you should use |
| 177 |
:py:meth:`aioftp.setlocale` context manager when you dealing with locale in |
| 178 |
another thread. |
| 179 |
**since 0.8.1** |
| 180 |
If fallback `LIST` parser can't parse line, then this line will be ignored, so |
| 181 |
fallback `LIST` implementation will never raise exception. But if requested |
| 182 |
path line can't be parsed, then :py:meth:`aioftp.Client.stat` method will |
| 183 |
raise `path does not exists`. |
| 184 |
|
| 185 |
|
| 186 |
Remove path |
| 187 |
----------- |
| 188 |
|
| 189 |
For removing paths you have universal coroutine :py:meth:`aioftp.Client.remove` |
| 190 |
which can remove file or directory recursive. So, you don't need to do borring |
| 191 |
checks. |
| 192 |
|
| 193 |
:: |
| 194 |
|
| 195 |
>>> await client.remove("tmp.py") |
| 196 |
>>> await client.remove("folder1") |
| 197 |
|
| 198 |
Dealing with directories |
| 199 |
------------------------ |
| 200 |
|
| 201 |
Directories coroutines are pretty simple. |
| 202 |
|
| 203 |
:py:meth:`aioftp.Client.get_current_directory` |
| 204 |
|
| 205 |
:py:meth:`aioftp.Client.change_directory` |
| 206 |
|
| 207 |
:py:meth:`aioftp.Client.make_directory` |
| 208 |
|
| 209 |
:: |
| 210 |
|
| 211 |
>>> await client.get_current_directory() |
| 212 |
PosixPath('/public_html') |
| 213 |
>>> await client.change_directory("folder1") |
| 214 |
>>> await client.get_current_directory() |
| 215 |
PosixPath('/public_html/folder1') |
| 216 |
>>> await client.change_directory() |
| 217 |
>>> await client.get_current_directory() |
| 218 |
PosixPath('/public_html') |
| 219 |
>>> await client.make_directory("folder2") |
| 220 |
>>> await client.change_directory("folder2") |
| 221 |
>>> await client.get_current_directory() |
| 222 |
PosixPath('/public_html/folder2') |
| 223 |
|
| 224 |
Rename (move) path |
| 225 |
------------------ |
| 226 |
|
| 227 |
To change name (move) file or directory use :py:meth:`aioftp.Client.rename`. |
| 228 |
|
| 229 |
:: |
| 230 |
|
| 231 |
>>> await client.list() |
| 232 |
[(PosixPath('test.py'), {'modify': '20150423090041', 'type': 'file', ... |
| 233 |
>>> await client.rename("test.py", "foo.py") |
| 234 |
>>> await client.list() |
| 235 |
[(PosixPath('foo.py'), {'modify': '20150423090041', 'type': 'file', ... |
| 236 |
|
| 237 |
Closing connection |
| 238 |
------------------ |
| 239 |
|
| 240 |
:py:meth:`aioftp.Client.quit` coroutine will send "QUIT" ftp command and close |
| 241 |
connection. |
| 242 |
|
| 243 |
:: |
| 244 |
|
| 245 |
>>> await client.quit() |
| 246 |
|
| 247 |
Advanced download and upload, abort, restart |
| 248 |
-------------------------------------------- |
| 249 |
|
| 250 |
File read/write operations are blocking and slow. So if you want just |
| 251 |
parse/calculate something on the fly when receiving file, or generate data |
| 252 |
to upload it to file system on ftp server, then you should use |
| 253 |
:py:meth:`aioftp.Client.download_stream`, |
| 254 |
:py:meth:`aioftp.Client.upload_stream` and |
| 255 |
:py:meth:`aioftp.Client.append_stream`. All this methods based on |
| 256 |
:py:meth:`aioftp.Client.get_stream`, which return |
| 257 |
:py:class:`aioftp.DataConnectionThrottleStreamIO`. The common pattern to |
| 258 |
work with streams is: |
| 259 |
|
| 260 |
:: |
| 261 |
|
| 262 |
>>> async with client.download_stream("tmp.py") as stream: |
| 263 |
... async for block in stream.iter_by_block(): |
| 264 |
... # do something with data |
| 265 |
|
| 266 |
Or, if you want to abort transfer at some point |
| 267 |
|
| 268 |
:: |
| 269 |
|
| 270 |
>>> stream = await client.download_stream("tmp.py") |
| 271 |
... async for block in stream.iter_by_block(): |
| 272 |
... # do something with data |
| 273 |
... if something_not_interesting: |
| 274 |
... await client.abort() |
| 275 |
... stream.close() |
| 276 |
... break |
| 277 |
... else: |
| 278 |
... await stream.finish() |
| 279 |
|
| 280 |
WARNING |
| 281 |
^^^^^^^ |
| 282 |
|
| 283 |
Do not use `async with <stream>` syntax if you want to use `abort`, this will |
| 284 |
lead to deadlock. |
| 285 |
|
| 286 |
For restarting upload/download at exact byte position (REST command) there is |
| 287 |
`offset` argument for `*_stream` methods: |
| 288 |
|
| 289 |
:: |
| 290 |
|
| 291 |
>>> async with client.download_stream("tmp.py", offset=256) as stream: |
| 292 |
... async for block in stream.iter_by_block(): |
| 293 |
... # do something with data |
| 294 |
|
| 295 |
Or if you want to restore upload/download process: |
| 296 |
|
| 297 |
:: |
| 298 |
|
| 299 |
>>> while True: |
| 300 |
... try: |
| 301 |
... async with aioftp.Client.context(HOST, PORT) as client: |
| 302 |
... if await client.exists(filename): |
| 303 |
... stat = await client.stat(filename) |
| 304 |
... size = int(stat["size"]) |
| 305 |
... else: |
| 306 |
... size = 0 |
| 307 |
... file_in.seek(size) |
| 308 |
... async with client.upload_stream(filename, offset=size) as stream: |
| 309 |
... while True: |
| 310 |
... data = file_in.read(block_size) |
| 311 |
... if not data: |
| 312 |
... break |
| 313 |
... await stream.write(data) |
| 314 |
... break |
| 315 |
... except ConnectionResetError: |
| 316 |
... pass |
| 317 |
|
| 318 |
The idea is to seek position of source «file» for upload and start |
| 319 |
upload + offset/append. Opposite situation for download («file» append and |
| 320 |
download + offset) |
| 321 |
|
| 322 |
Throttle |
| 323 |
-------- |
| 324 |
|
| 325 |
Client have two types of speed limit: `read_speed_limit` and |
| 326 |
`write_speed_limit`. Throttle can be set at initialization time: |
| 327 |
|
| 328 |
:: |
| 329 |
|
| 330 |
>>> client = aioftp.Client(read_speed_limit=100 * 1024) # 100 Kib/s |
| 331 |
|
| 332 |
And can be changed after creation: |
| 333 |
|
| 334 |
:: |
| 335 |
|
| 336 |
>>> client.throttle.write.limit = 250 * 1024 |
| 337 |
|
| 338 |
Path abstraction layer |
| 339 |
---------------------- |
| 340 |
|
| 341 |
aioftp provides abstraction of file system operations. You can use exist ones: |
| 342 |
|
| 343 |
* :py:class:`aioftp.PathIO` — blocking path operations |
| 344 |
* :py:class:`aioftp.AsyncPathIO` — non-blocking path operations, this one is |
| 345 |
blocking ones just wrapped with |
| 346 |
:py:meth:`asyncio.BaseEventLoop.run_in_executor`. It's really slow, so it's |
| 347 |
better to avoid usage of this path io layer. |
| 348 |
* :py:class:`aioftp.MemoryPathIO` — in-memory realization of file system, this |
| 349 |
one is just proof of concept and probably not too fast (as it can be). |
| 350 |
|
| 351 |
You can specify `path_io_factory` when creating :py:class:`aioftp.Client` |
| 352 |
instance. Default factory is :py:class:`aioftp.PathIO`. |
| 353 |
|
| 354 |
:: |
| 355 |
|
| 356 |
>>> client = aioftp.Client(path_io_factory=pathio.MemoryPathIO) |
| 357 |
|
| 358 |
Timeouts |
| 359 |
-------- |
| 360 |
|
| 361 |
:py:class:`aioftp.Client` have `socket_timeout` argument, which you can use |
| 362 |
to specify global timeout for socket io operations. |
| 363 |
|
| 364 |
:: |
| 365 |
|
| 366 |
>>> client = aioftp.Client(socket_timeout=1) # 1 second socket timeout |
| 367 |
|
| 368 |
:py:class:`aioftp.Client` also have `path_timeout`, which is applied |
| 369 |
**only for non-blocking path io layers**. |
| 370 |
|
| 371 |
:: |
| 372 |
|
| 373 |
>>> client = aioftp.Client( |
| 374 |
... path_timeout=1, |
| 375 |
... path_io_factory=pathio.AsyncPathIO |
| 376 |
... ) |
| 377 |
|
| 378 |
TLS Upgrade Support |
| 379 |
------------------- |
| 380 |
|
| 381 |
Just like Python's `ftplib.FTP_TLS`, aioftp supports TLS upgrade. This is |
| 382 |
done by calling :py:meth:`aioftp.Client.upgrade_to_tls` after instantiating the |
| 383 |
client, like: |
| 384 |
|
| 385 |
:: |
| 386 |
|
| 387 |
>>> client = aioftp.Client() |
| 388 |
>>> await client.connect("ftp.server.com") |
| 389 |
>>> await client.upgrade_to_tls() |
| 390 |
>>> await client.login("user", "pass") |
| 391 |
|
| 392 |
Using proxy |
| 393 |
----------- |
| 394 |
|
| 395 |
Simplest way to use socks proxy with :class:`aioftp.Client` |
| 396 |
is `siosocks <https://github.com/pohmelie/siosocks>`_ |
| 397 |
|
| 398 |
:: |
| 399 |
|
| 400 |
>>> client = aioftp.Client( |
| 401 |
... socks_host="localhost", |
| 402 |
... socks_port=9050, |
| 403 |
... socks_version=5, |
| 404 |
... ) |
| 405 |
|
| 406 |
Don't forget to install `aioftp` as `pip install aioftp[socks]`, or install |
| 407 |
`siosocks` directly with `pip install siosocks`. |
| 408 |
|
| 409 |
WARNING |
| 410 |
------- |
| 411 |
|
| 412 |
:py:meth:`aioftp.Client.list` and :py:meth:`aioftp.Client.stat` in general |
| 413 |
use `MLSD` and `MLST`, but some nasty servers does not support this commands. |
| 414 |
Then client will try to use `LIST` command, and parse server response. |
| 415 |
For proper work of :py:meth:`datetime.datetime.strptime` (in part of parsing |
| 416 |
month abbreviation) locale should be setted to "C". For this reason if you use |
| 417 |
multithreaded app, and use some locale-dependent stuff, you should use |
| 418 |
:py:meth:`aioftp.setlocale` context manager when you dealing with locale in |
| 419 |
another thread. |
| 420 |
**since 0.8.1** |
| 421 |
If fallback `LIST` parser can't parse line, then this line will be ignored, so |
| 422 |
fallback `LIST` implementation will never raise exception. |
| 423 |
|
| 424 |
Futher reading |
| 425 |
-------------- |
| 426 |
:doc:`client_api` |