source: sandbox/session.py @ 1486:f70b18e91500

Last change on this file since 1486:f70b18e91500 was 1486:f70b18e91500, checked in by Stefan Schwarzer <sschwarzer@…>, 7 years ago
Added "universal" "session factory factory". The new module `session.py` defines a function `session_factory` to create a session factory (class) according to the keyword arguments passed to the function. Example: my_session_factory = session_factory( base_class=M2Crypto.ftpslib.FTP_TLS, use_passive_mode=True, encrypt_data_channel=True) with ftputil.FTPHost(host, user, password, session_factory=my_session_factory) as host:
File size: 2.9 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    Usage example:
43
44      my_session_factory = session_factory(
45                             base_class=M2Crypto.ftpslib.FTP_TLS,
46                             use_passive_mode=True,
47                             encrypt_data_channel=True)
48      with ftputil.FTPHost(host, user, password,
49                           session_factory=my_session_factory) as host:
50        ...
51    """
52    class Session(base_class):
53        """Session factory class created by `session_factory`."""
54
55        def __init__(self, host, user, password):
56            base_class.__init__(self)
57            self.connect(host, port)
58            if self._use_m2crypto_ftpslib():
59                self.auth_tls()
60                self._fix_socket()
61            self.login(user, password)
62            if use_passive_mode is not None:
63                self.set_pasv(use_passive_mode)
64            if encrypt_data_channel:
65                self.prot_p()
66
67        def _use_m2crypto_ftpslib(self):
68            """
69            Return `True` if the base class to use is
70            `M2Crypto.ftpslib.FTP_TLS`, else return `False`.
71            """
72            return (M2Crypto is not None and
73                    issubclass(base_class, M2Crypto.ftpslib.FTP_TLS))
74
75        def _fix_socket(self):
76            """
77            Change the socket object so that arguments to `sendall`
78            are converted to byte strings before being used.
79
80            See the ftputil ticket #78 for details:
81            http://ftputil.sschwarzer.net/trac/ticket/78
82            """
83            original_sendall = self.sock.sendall
84            # Bound method, therefore no `self` argument.
85            def sendall(data):
86                data = ftputil.tool.as_bytes(data)
87                return original_sendall(data)
88            self.sock.sendall = sendall
89
90    return Session
Note: See TracBrowser for help on using the repository browser.