简体   繁体   中英

How to pass an escape character in password used to connect to a FTP server using Python ftplib library?

I am trying to connect to a FTP server using the standard Python FTP library (ftplib). I am using Python 3.8.

The account used to access the FTP server has a password containing the escape character \ which is automatically doubled once read by ftplib.

So in my password, among all the characters, I have one \ but ftplib sends \ instead. The password is automatically generated according a security policy so I cannot request to avoid this kind of character.

How can I tell to ftplib to take the string as is?

I already tried r'mypwd' or r'{}'.format(mypwd) without any success. Each time I look in the ftp object in the debug I see the \ has been doubled.

Thank you for the help!

EDIT:

Example:

from ftplib import FTP_TLS

addr = '192.168.1.23'
usr = 'ftpuser'
pwd = '3Hc]85}Lxqy\%I+bc1(T'

ftp = FTP_TLS()
ftp.set_debuglevel(2)
ftp.connect(host=addr, port=21)
ftp.login(user=usr, passwd=pwd)

When you set a breakpoint on ftp = FTP(), you can see the password has been altered

debug screenshot

EDIT 2: Log from Filezilla:

Status:         Connecting to 192.168.1.23:21...
Status:         Connection established, waiting for welcome message...
Status:         Insecure server, it does not support FTP over TLS.
Status:         Server does not support non-ASCII characters.
Status:         Logged in
Status:         Retrieving directory listing...
Status:         Directory listing of "/home/ftpuser" successful
16:50:39 Status:        Disconnected from server
16:50:39 Status:        Connecting to 192.168.1.23:21...
16:50:39 Status:        Connection established, waiting for welcome message...
16:50:39 Response:  220 (vsFTPd 3.0.3)
16:50:39 Command:   AUTH TLS
16:50:39 Response:  530 Please login with USER and PASS.
16:50:39 Command:   AUTH SSL
16:50:39 Response:  530 Please login with USER and PASS.
16:50:39 Status:        Insecure server, it does not support FTP over TLS.
16:50:39 Command:   USER ftpuser
16:50:39 Response:  331 Please specify the password.
16:50:39 Command:   PASS ********************
16:50:39 Response:  230 Login successful.
16:50:39 Status:        Server does not support non-ASCII characters.
16:50:39 Status:        Logged in
16:50:39 Status:        Retrieving directory listing...
16:50:39 Command:   PWD
16:50:39 Response:  257 "/home/ftpuser" is the current directory
16:50:39 Status:        Directory listing of "/home/ftpuser" successful

Log from ftplib:

*get* '220 (vsFTPd 3.0.3)\n'
*resp* '220 (vsFTPd 3.0.3)'
*cmd* 'AUTH TLS'
*put* 'AUTH TLS\r\n'
*get* '530 Please login with USER and PASS.\n'
*resp* '530 Please login with USER and PASS.'
Traceback (most recent call last):
  File "/xxxxxx/test.py", line 10, in <module>
    ftp.login(user=usr, passwd=pwd)
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/ftplib.py", line 739, in login
    self.auth()
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/ftplib.py", line 747, in auth
    resp = self.voidcmd('AUTH TLS')
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/ftplib.py", line 282, in voidcmd
    return self.voidresp()
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/ftplib.py", line 255, in voidresp
    resp = self.getresp()
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/ftplib.py", line 250, in getresp
    raise error_perm(resp)
ftplib.error_perm: 530 Please login with USER and PASS.

From the AUTH TLS record in the log file, it looks like you are actually trying to use FTP_TLS class (not FTP class, despite your code example), against a server does does not support encryption.

Use FTP , not FTP_TLS .

Thanks for your feedback. I figured out my mistake.

from ftplib import FTP

addr = '192.168.1.23'
usr = 'ftpuser'
pwd = '3Hc]85}Lxqy\%I+bc1(T'

ftp = FTP()
ftp.set_debuglevel(2)
ftp.connect(host=addr, port=21)
ftp.login(user=usr, passwd=pwd)
ftp.login()

The initial piece of code was this above. Actually I jumped to the last message "error 530" whithout really reading the full logs. It is because of my last line ftp.login() I copied/pasted from the example in the python doc ( https://docs.python.org/3.8/library/ftplib.html ) despite I added the ftp.login(user=usr, passwd=pwd) to connect before.

Thank you for helping me on my mistake...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM