Changeset 1720:a10aefe0f71f
- Timestamp:
- Dec 25, 2018, 11:31:04 PM (2 years ago)
- Branch:
- default
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
ftputil/session.py
r1713 r1720 11 11 12 12 import ftputil.tool 13 14 try:15 import M2Crypto16 import M2Crypto.ftpslib17 except ImportError:18 M2Crypto = None19 13 20 14 … … 53 47 54 48 This function should work for the base classes `ftplib.FTP`, 55 `ftplib.FTP_TLS` and `M2Crypto.ftpslib.FTP_TLS` with TLS security. 56 Other base classes should work if they use the same API as 57 `ftplib.FTP`. 49 `ftplib.FTP_TLS`. Other base classes should work if they use the 50 same API as `ftplib.FTP`. 58 51 59 52 Usage example: 60 53 61 54 my_session_factory = session_factory( 62 base_class= M2Crypto.ftpslib.FTP_TLS,55 base_class=ftplib.FTP_TLS, 63 56 use_passive_mode=True, 64 57 encrypt_data_channel=True) … … 71 64 72 65 def __init__(self, host, user, password): 73 # Don't use `super` in case `base_class` isn't a new-style 74 # class (e. g. `ftplib.FTP` in Python 2). 75 base_class.__init__(self) 66 super().__init__() 76 67 self.connect(host, port) 77 if self._use_m2crypto_ftpslib():78 self.auth_tls()79 self._fix_socket()80 68 if debug_level is not None: 81 69 self.set_debuglevel(debug_level) … … 88 76 self.prot_p() 89 77 90 def _use_m2crypto_ftpslib(self):91 """92 Return `True` if the base class to use is93 `M2Crypto.ftpslib.FTP_TLS`, else return `False`.94 """95 return (M2Crypto is not None and96 issubclass(base_class, M2Crypto.ftpslib.FTP_TLS))97 98 def _fix_socket(self):99 """100 Change the socket object so that arguments to `sendall`101 are converted to byte strings before being used.102 103 See the ftputil ticket #78 for details:104 http://ftputil.sschwarzer.net/trac/ticket/78105 """106 original_sendall = self.sock.sendall107 # Bound method, therefore no `self` argument.108 def sendall(data):109 data = ftputil.tool.as_bytes(data)110 return original_sendall(data)111 self.sock.sendall = sendall112 113 78 return Session -
test/test_real_ftp.py
r1719 r1720 964 964 objects_after_test = len(gc.garbage) 965 965 assert not objects_after_test - objects_before_test 966 967 @pytest.mark.skipif(968 ftputil.compat.python_version > 2,969 reason="test requires M2Crypto which only works on Python 2")970 def test_m2crypto_session(self):971 """972 Test if a session with `M2Crypto.ftpslib.FTP_TLS` is set up973 correctly and works with unicode input.974 """975 # See ticket #78.976 #977 # M2Crypto is only available for Python 2.978 import M2Crypto979 factory = ftputil.session.session_factory(980 base_class=M2Crypto.ftpslib.FTP_TLS,981 encrypt_data_channel=True)982 with ftputil.FTPHost(*self.login_data, session_factory=factory) as host:983 # Test if unicode argument works.984 files = host.listdir(".")985 assert "CONTENTS" in files -
test/test_session.py
r1718 r1720 24 24 def connect(self, host, port): 25 25 self.add_call("connect", host, port) 26 27 def _fix_socket(self):28 self.add_call("_fix_socket")29 26 30 27 def set_debuglevel(self, value): … … 120 117 ("set_debuglevel", 1), 121 118 ("login", "user", "password")] 122 123 def test_m2crypto_session(self):124 """Test call sequence for M2Crypto session."""125 factory = \126 ftputil.session.session_factory(base_class=EncryptedMockSession)127 # Return `True` to fake that this is a session deriving from128 # `M2Crypto.ftpslib.FTP_TLS`.129 factory._use_m2crypto_ftpslib = lambda self: True130 # Override `_fix_socket` here, not in `MockSession`. Since131 # the created session class _inherits_ from `MockSession`,132 # it would override the `_fix_socket` there.133 factory._fix_socket = lambda self: self.add_call("_fix_socket")134 session = factory("host", "user", "password")135 assert session.calls == [("connect", "host", 21),136 ("auth_tls",),137 ("_fix_socket",),138 ("login", "user", "password"),139 ("prot_p",)] -
tox.ini
r1695 r1720 5 5 6 6 [tox] 7 #envlist = py 27, py34, pypy8 envlist = py 27, py357 #envlist = py34, pypy 8 envlist = py35, py36, py37 9 9 10 10 [testenv] 11 commands = py .test test11 commands = python -m pytest test 12 12 deps = 13 13 pytest 14 15 [testenv:py27]16 # setenv =17 # # Used (hopefully) temporarily since M2Crypto build fails with18 # # SWIG error19 # PYTHONPATH=/usr/lib64/python2.7/site-packages20 deps =21 pytest22 M2Crypto
Note: See TracChangeset
for help on using the changeset viewer.