我有一个有趣的问题-我需要通过Python3
代码连接到FTP / SFTP服务器,但是密码内部包含\n
符号。当我将密码粘贴到FTP / SFTP bash客户端中时,它可以工作。但是,如果我使用pysftp
或ftplib
,则验证失败。
当然,我可以使用pexpect
来模拟bash环境中的所有事物,但是也许有一种更Python风格的方式来实现它?
我使用Ubuntu 18.10
,ftp v. 0.17-34
,pysftp 0.2.9
或paramiko 2.4.2
。
这就是我试图做的
import paramiko
transport = paramiko.Transport('servername', 22)
password = "why\ninside"
username = anyuser
transport.connect(username=username, password=password)
那就是我得到的
---------------------------------------------------------------------------
AuthenticationException Traceback (most recent call last)
<ipython-input-7-2bf86c34be75> in <module>()
----> 1 transport.connect(username=username, password=password)
/home/vasily/.local/lib/python3.6/site-packages/paramiko/transport.py in connect(self, hostkey, username, password, pkey, gss_host, gss_auth, gss_kex, gss_deleg_creds, gss_trust_dns)
1261 else:
1262 self._log(DEBUG, "Attempting password auth...")
-> 1263 self.auth_password(username, password)
1264
1265 return
/home/vasily/.local/lib/python3.6/site-packages/paramiko/transport.py in auth_password(self, username, password, event, fallback)
1434 return []
1435 try:
-> 1436 return self.auth_handler.wait_for_response(my_event)
1437 except BadAuthenticationType as e:
1438 # if password auth isn't allowed, but keyboard-interactive *is*,
/home/vasily/.local/lib/python3.6/site-packages/paramiko/auth_handler.py in wait_for_response(self, event)
248 if issubclass(e.__class__, PartialAuthentication):
249 return e.allowed_types
--> 250 raise e
251 return []
252
AuthenticationException: Authentication failed.
但是如果我使用pexpect
并通过FTP或SFTP发送该密码,就可以了。
答案 0 :(得分:-1)
使用它;
password = r"why\ninside"