source: sandbox/session.py @ 1487:4a0098ea7ecb

Last change on this file since 1487:4a0098ea7ecb was 1487:4a0098ea7ecb, checked in by Stefan Schwarzer <sschwarzer@…>, 7 years ago
Document the anticipated session base classes.
File size: 3.1 KB
Line 
1# Copyright (C) 2014, Stefan Schwarzer
2
3"""
4Session factory factory (the two "factory" are intential :-) )
5for ftputil.
6"""
7
8import ftplib
9
10import ftputil.tool
11
12try:
13    import M2Crypto
14    import M2Crypto.ftpslib
15except ImportError:
16    M2Crypto = None
17
18
19def session_factory(base_class=ftplib.FTP, port=21, use_passive_mode=None,
20                    encrypt_data_channel=None):
21    """
22    Create and return a session factory according to the keyword
23    arguments.
24
25    base_class: Base class to use for the session class (e. g.
26    `ftplib.FTP_TLS` or `M2Crypto.ftpslib.FTP_TLS`, default is
27    `ftplib.FTP`).
28
29    port: Port number (integer) for the command channel (default 21).
30    If you don't know what "command channel" means, use the default or
31    use what the provider gave you as "the FTP port".
32
33    use_passive_mode: If `True`, explicitly use passive mode. If
34    `False`, explicitly don't use passive mode. If `None` (default),
35    let the `base_class` decide whether it wants to use active or
36    passive mode.
37
38    encrypt_data_channel: If `True`, call the `prot_p` method of the
39    base class. If `False` or `None` (`None` is the default), don't
40    call the method.
41
42    This function should work the base classes for `ftplib.FTP`,
43    `ftplib.FTP_TLS` and `M2Crypto.ftpslib.FTP_TLS` with TLS security.
44    Other base classes should work if they use the same API as
45    `ftplib.FTP`.
46
47    Usage example:
48
49      my_session_factory = session_factory(
50                             base_class=M2Crypto.ftpslib.FTP_TLS,
51                             use_passive_mode=True,
52                             encrypt_data_channel=True)
53      with ftputil.FTPHost(host, user, password,
54                           session_factory=my_session_factory) as host:
55        ...
56    """
57    class Session(base_class):
58        """Session factory class created by `session_factory`."""
59
60        def __init__(self, host, user, password):
61            base_class.__init__(self)
62            self.connect(host, port)
63            if self._use_m2crypto_ftpslib():
64                self.auth_tls()
65                self._fix_socket()
66            self.login(user, password)
67            if use_passive_mode is not None:
68                self.set_pasv(use_passive_mode)
69            if encrypt_data_channel:
70                self.prot_p()
71
72        def _use_m2crypto_ftpslib(self):
73            """
74            Return `True` if the base class to use is
75            `M2Crypto.ftpslib.FTP_TLS`, else return `False`.
76            """
77            return (M2Crypto is not None and
78                    issubclass(base_class, M2Crypto.ftpslib.FTP_TLS))
79
80        def _fix_socket(self):
81            """
82            Change the socket object so that arguments to `sendall`
83            are converted to byte strings before being used.
84
85            See the ftputil ticket #78 for details:
86            http://ftputil.sschwarzer.net/trac/ticket/78
87            """
88            original_sendall = self.sock.sendall
89            # Bound method, therefore no `self` argument.
90            def sendall(data):
91                data = ftputil.tool.as_bytes(data)
92                return original_sendall(data)
93            self.sock.sendall = sendall
94
95    return Session
Note: See TracBrowser for help on using the repository browser.