File: server_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 (175 lines) | stat: -rw-r--r-- 4,962 bytes parent folder | download | duplicates (4)
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
.. server_tutorial:

Server tutorial
===============

aioftp server is much more like a tool. You configure it, run and forget about
it.

Configuring server
------------------

At first you should create :class:`aioftp.Server` instance and start it

:py:meth:`aioftp.Server.start`

::

    >>> server = aioftp.Server()
    >>> await server.start()

Default arguments allow anonymous login and read/write current directory. So,
there is one user with anonymous login and read/write permissions on "/"
virtual path. Real path is current working directory.

Dealing with users and permissions
----------------------------------

You can specify as much users as you want, just pass list of them when creating
:class:`aioftp.Server` instance

:class:`aioftp.User`

:class:`aioftp.Permission`

::

    >>> users = (
    ...     aioftp.User(
    ...         "Guido",
    ...         "secret_password",
    ...         home_path="/Guido",
    ...         permissions=(
    ...             aioftp.Permission("/", readable=False, writable=False),
    ...             aioftp.Permission("/Guido", readable=True, writable=True),
    ...         )
    ...     ),
    ...     aioftp.User(
    ...         home_path="/anon",
    ...         permissions=(
    ...             aioftp.Permission("/", readable=False, writable=False),
    ...             aioftp.Permission("/anon", readable=True),
    ...         )
    ...     ),
    ... )
    >>> server = aioftp.Server(users)
    >>> await server.start()

This will create two users: "Guido", who can read and write to "/Guido" folder,
which is home folder, but can't read/write the root and other directories and
anonymous user, who home directory is "/anon" and there is only read
permission.

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.Server`
instance. Default factory is :py:class:`aioftp.PathIO`.

::

    >>> server = aioftp.Server(path_io_factory=aioftp.MemoryPathIO)
    >>> await server.start()

Dealing with timeouts
---------------------

There is three different timeouts you can specify:

* `socket_timeout` — timeout for low-level socket operations
  :py:meth:`asyncio.StreamReader.read`,
  :py:meth:`asyncio.StreamReader.readline` and
  :py:meth:`asyncio.StreamWriter.drain`. This one does not affects awaiting
  command read operation.
* `path_timeout` — timeout for file system operations
* `idle_timeout` — timeout for socket read operation when awaiting command,
  another words: how long user can keep silence without sending commands
* `wait_future_timeout` — timeout for waiting connection states (the main
  purpose is wait for passive connection)

Maximum connections
-------------------

Connections count can be specified:

* per server
* per user

First one via server constructor

::

    >>> server = aioftp.Server(maximum_connections=3)

Second one via user class

::

    >>> users = (aioftp.User(maximum_connections=3),)
    >>> server = aioftp.Server(users)

Throttle
--------

Server have many options for read/write speed throttle:

* global per server
* per connection
* global per user
* per user connection

"Global per server" and "per connection" can be provided by constructor

::

    >>> server = aioftp.Server(
    ...     read_speed_limit=1024 * 1024,
    ...     write_speed_limit=1024 * 1024,
    ...     read_speed_limit_per_connection=100 * 1024,
    ...     write_speed_limit_per_connection=100 * 1024
    ... )

User throttles can be provided by user constructor

::

    >>> users = (
    ...     aioftp.User(
    ...         read_speed_limit=1024 * 1024,
    ...         write_speed_limit=1024 * 1024,
    ...         read_speed_limit_per_connection=100 * 1024,
    ...         write_speed_limit_per_connection=100 * 1024
    ...     ),
    ... )
    >>> server = aioftp.Server(users)

Stopping the server
-------------------

::

    >>> await server.close()

WARNING
-------

:py:meth:`aioftp.Server.list` use :py:meth:`aioftp.Server.build_list_string`,
which should produce `LIST` strings with :py:meth:`datetime.datetime.strftime`.
For proper work (in part of formatting 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.

Futher reading
--------------
:doc:`server_api`