def setup_ftp_users(self): # Define a new user with full r/w permissions return ( aioftp.User(str(self.user), str(self.pword), home_path=self.home, permissions=( aioftp.Permission('/', readable=False, writable=False), aioftp.Permission(self.home, readable=True, writable=True), ), maximum_connections=MAX_CONNECTIONS, read_speed_limit=SPEED_LIMIT, write_speed_limit=SPEED_LIMIT, read_speed_limit_per_connection=SPEED_LIMIT_PER_CONN, write_speed_limit_per_connection=SPEED_LIMIT_PER_CONN), aioftp.User(home_path=ANON_HOME_PATH, permissions=( aioftp.Permission('/', readable=False, writable=False), aioftp.Permission(ANON_HOME_PATH, readable=True), ), maximum_connections=MAX_ANON_CONNECTIONS, read_speed_limit=SPEED_LIMIT, write_speed_limit=SPEED_LIMIT, read_speed_limit_per_connection=SPEED_LIMIT_PER_CONN, write_speed_limit_per_connection=SPEED_LIMIT_PER_CONN), )
async def test_pass_after_login(pair_factory, Server, expect_codes_in_exception): s = Server([aioftp.User("foo", "bar")]) async with pair_factory(None, s, logged=False) as pair: await pair.client.login("foo", "bar") with expect_codes_in_exception("503"): await pair.client.command("PASS baz", ("230", "33x"))
async def test_permission_denied(pair_factory, Server, expect_codes_in_exception): s = Server([ aioftp.User(permissions=[aioftp.Permission(writable=False)]) ]) async with pair_factory(None, s) as pair: with expect_codes_in_exception("550"): await pair.client.make_directory("foo")
async def test_permission_overriden(pair_factory, Server): s = Server([ aioftp.User( permissions=[ aioftp.Permission("/", writable=False), aioftp.Permission("/foo"), ] ) ]) async with pair_factory(None, s) as pair: await pair.client.make_directory("foo") await pair.client.remove_directory("foo")
def __init__(self, loop, host='localhost', port=2221, user=None, password=None): if user: users = aioftp.User(user, password), self.server = aioftp.Server(users, loop=loop) else: # Setup server with anonymous login self.server = aioftp.Server(loop=loop) self.host = host self.port = port
async def test_multiply_connections_with_user_limited_error( pair_factory, Server, expect_codes_in_exception): Client = functools.partial(aioftp.Client, path_io_factory=aioftp.MemoryPathIO) s = Server([aioftp.User("foo", maximum_connections=4)]) async with pair_factory(None, s, connected=False) as pair: s = pair.server clients = [Client() for _ in range(5)] for c in clients[:-1]: await c.connect(s.server_host, s.server_port) await c.login("foo") await clients[-1].connect(s.server_host, s.server_port) with expect_codes_in_exception("530"): await clients[-1].login("foo") for c in clients[:-1]: await c.quit()
def test_user_repr(): u = aioftp.User() nose.tools.eq_( repr(u), "User(" "None, " "None, " "base_path=PosixPath('.'), " "home_path=PurePosixPath('/'), " "permissions=[" "Permission(PurePosixPath('/'), " "readable=True, " "writable=True)], " "maximum_connections=None, " "read_speed_limit=None, " "write_speed_limit=None, " "read_speed_limit_per_connection=None, " "write_speed_limit_per_connection=None)")
def test_reprs_works(): repr(aioftp.Throttle()) repr(aioftp.Permission()) repr(aioftp.User())
parser.add_argument("--port", type=int, default=2121, help="port for binding [default: %(default)s]") args = parser.parse_args() print("aioftp v{}".format(aioftp.__version__)) if not args.quiet: logging.basicConfig( level=logging.INFO, format="%(asctime)s [%(name)s] %(message)s", datefmt="[%H:%M:%S]:", ) if args.memory: user = aioftp.User(args.login, args.password, base_path="/") path_io_factory = aioftp.MemoryPathIO else: if args.home: user = aioftp.User(args.login, args.password, base_path=args.home) else: user = aioftp.User(args.login, args.password) path_io_factory = aioftp.PathIO server = aioftp.Server([user], path_io_factory=path_io_factory) loop = asyncio.get_event_loop() loop.run_until_complete(server.start(args.host, args.port)) try: loop.run_forever() except KeyboardInterrupt:
def test_user_not_absolute_home(): with pytest.raises(aioftp.errors.PathIsNotAbsolute): aioftp.User(home_path="foo")
@with_connection async def test_multiply_connections_no_limits(loop, client, server): clients = [aioftp.Client(loop=loop) for _ in range(4)] for client in clients: await client.connect("127.0.0.1", PORT) await client.login() for client in clients: await client.quit() @aioftp_setup( server_args=([(aioftp.User(maximum_connections=4),)], {})) @expect_codes_in_exception("530") @with_connection async def test_multiply_connections_limited_error(loop, client, server): clients = [aioftp.Client(loop=loop) for _ in range(5)] for client in clients: await client.connect("127.0.0.1", PORT) await client.login() for client in clients: await client.quit()
import docopt import aioftp args = docopt.docopt(__doc__, version=aioftp.__version__) print(str.format("aioftp v{}", aioftp.__version__)) if not args["--quiet"]: logging.basicConfig( level=logging.INFO, format="%(asctime)s %(message)s", datefmt="[%H:%M:%S]:", ) user = aioftp.User(args["<login>"], args["<password>"]) server = aioftp.Server([user]) loop = asyncio.get_event_loop() loop.run_until_complete(server.start(args["--host"], int(args["--port"]))) try: loop.run_forever() except KeyboardInterrupt: server.close() loop.run_until_complete(server.wait_closed()) loop.close()
import nose import contextlib import aioftp.client import aioftp from common import * # noqa from aioftp.server import MemoryUserManager @nose.tools.raises(ConnectionResetError) @aioftp_setup( server_args=( [(aioftp.User(base_path="tests/foo", home_path="/"),)], {"idle_timeout": 1})) @with_connection def test_idle_timeout(loop, client, server): yield from asyncio.sleep(2, loop=loop) yield from client.login() class SlowMemoryPathIO(aioftp.MemoryPathIO): @aioftp.with_timeout @asyncio.coroutine def mkdir(self, path, parents=False): yield from asyncio.sleep(10, loop=self.loop) @aioftp.with_timeout @asyncio.coroutine
fout.write(b"-" * 9 * 100 * 1024) start = time.perf_counter() await asyncio.wait(map(worker, fnames), loop=loop) nose.tools.ok_(4 < (time.perf_counter() - start) < 5) big_file.unlink() for fname in fnames: received = tmp_dir / fname received.unlink() @aioftp_setup(server_args=( [[aioftp.User(write_speed_limit_per_connection=200 * 1024)]], {}, )) @with_connection @with_tmp_dir("foo") async def test_server_user_per_connection_write_throttle_multi_users( loop, client, server, *, tmp_dir): async def worker(fname): client = aioftp.Client(loop=loop) await client.connect("127.0.0.1", PORT) await client.login() await client.download("tests/foo/foo.txt", str.format("tests/foo/{}", fname), write_into=True) await client.quit()
async def test_login_with_login_and_password(pair_factory, Server): s = Server([aioftp.User("foo", "bar")]) async with pair_factory(None, s, logged=False) as pair: await pair.client.login("foo", "bar")
def test_user_not_absolute_home(): aioftp.User(home_path="foo")
import nose import contextlib import aioftp.client import aioftp from common import * # noqa from aioftp.server import MemoryUserManager @nose.tools.raises(ConnectionResetError) @aioftp_setup(server_args=([(aioftp.User(base_path="tests/foo", home_path="/"), )], { "idle_timeout": 1 })) @with_connection async def test_idle_timeout(loop, client, server): await asyncio.sleep(2, loop=loop) await client.login() class SlowMemoryPathIO(aioftp.MemoryPathIO): @aioftp.pathio.universal_exception @aioftp.with_timeout async def mkdir(self, path, parents=False): await asyncio.sleep(10, loop=self.loop) @aioftp.pathio.universal_exception @aioftp.with_timeout async def _open(self, path, mode):
import nose from common import * # noqa import aioftp @aioftp_setup( server_args=([(aioftp.User(base_path="tests/foo"),)], {})) @with_connection @with_tmp_dir("foo") async def test_restart_retr_0(loop, client, server, *, tmp_dir): tmp_file = tmp_dir / "foo.txt" with tmp_file.open(mode="w") as fout: fout.write("foobar") await client.login() async with client.download_stream("foo.txt", offset=0) as stream: r = await stream.read() await client.quit() tmp_file.unlink() nose.tools.eq_(r, b"foobar") @aioftp_setup( server_args=([(aioftp.User(base_path="tests/foo"),)], {})) @with_connection
async def test_login_with_login_and_password_bad_password( pair_factory, Server, expect_codes_in_exception): s = Server([aioftp.User("foo", "bar")]) async with pair_factory(None, s, logged=False) as pair: with expect_codes_in_exception("530"): await pair.client.login("foo", "baz")