source: ftputil/session.py @ 1720:a10aefe0f71f

Last change on this file since 1720:a10aefe0f71f was 1720:a10aefe0f71f, checked in by Stefan Schwarzer <sschwarzer@…>, 7 months ago
Remove support for `M2Crypto` M2Crypto was used to get FTP_TLS support in Python 2. Since we now target Python 3.5+, users can and should use `ftplib.FTP_TLS` to get FTP_TLS support.
File size: 2.8 KB
Line 
1# Copyright (C) 2014-2018, Stefan Schwarzer <sschwarzer@sschwarzer.net>
2# and ftputil contributors (see `doc/contributors.txt`)
3# See the file LICENSE for licensing terms.
4
5"""
6Session factory factory (the two "factory" are intential :-) )
7for ftputil.
8"""
9
10import ftplib
11
12import ftputil.tool
13
14
15__all__ = ["session_factory"]
16
17
18# In a way, it would be appropriate to call this function
19# `session_factory_factory`, but that's cumbersome to use. Think of
20# the function returning a session factory and the shorter name should
21# be fine.
22def session_factory(base_class=ftplib.FTP, port=21, use_passive_mode=None,
23                    encrypt_data_channel=True, debug_level=None):
24    """
25    Create and return a session factory according to the keyword
26    arguments.
27
28    base_class: Base class to use for the session class (e. g.
29    `ftplib.FTP_TLS` or `M2Crypto.ftpslib.FTP_TLS`, default is
30    `ftplib.FTP`).
31
32    port: Port number (integer) for the command channel (default 21).
33    If you don't know what "command channel" means, use the default or
34    use what the provider gave you as "the FTP port".
35
36    use_passive_mode: If `True`, explicitly use passive mode. If
37    `False`, explicitly don't use passive mode. If `None` (default),
38    let the `base_class` decide whether it wants to use active or
39    passive mode.
40
41    encrypt_data_channel: If `True` (the default), call the `prot_p`
42    method of the base class if it has the method. If `False` or
43    `None` (`None` is the default), don't call the method.
44
45    debug_level: Debug level (integer) to be set on a session
46    instance. The default is `None`, meaning no debugging output.
47
48    This function should work for the base classes `ftplib.FTP`,
49    `ftplib.FTP_TLS`. Other base classes should work if they use the
50    same API as `ftplib.FTP`.
51
52    Usage example:
53
54      my_session_factory = session_factory(
55                             base_class=ftplib.FTP_TLS,
56                             use_passive_mode=True,
57                             encrypt_data_channel=True)
58      with ftputil.FTPHost(host, user, password,
59                           session_factory=my_session_factory) as host:
60        ...
61    """
62    class Session(base_class):
63        """Session factory class created by `session_factory`."""
64
65        def __init__(self, host, user, password):
66            super().__init__()
67            self.connect(host, port)
68            if debug_level is not None:
69                self.set_debuglevel(debug_level)
70            self.login(user, password)
71            # `set_pasv` can be called with `True` (causing passive
72            # mode) or `False` (causing active mode).
73            if use_passive_mode is not None:
74                self.set_pasv(use_passive_mode)
75            if encrypt_data_channel and hasattr(base_class, "prot_p"):
76                self.prot_p()
77
78    return Session
Note: See TracBrowser for help on using the repository browser.