如何在任何python ftp连接中放入带有“ \ n”的密码?

时间:2019-04-18 07:10:31

标签: python ftp sftp ftplib pysftp

我有一个有趣的问题-我需要通过Python3代码连接到FTP / SFTP服务器,但是密码内部包含\n符号。当我将密码粘贴到FTP / SFTP bash客户端中时,它可以工作。但是,如果我使用pysftpftplib,则验证失败。 当然,我可以使用pexpect来模拟bash环境中的所有事物,但是也许有一种更Python风格的方式来实现它?

我使用Ubuntu 18.10ftp v. 0.17-34pysftp 0.2.9paramiko 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发送该密码,就可以了。

1 个答案:

答案 0 :(得分:-1)

使用它;

password = r"why\ninside"