1

I want to connect to my FTPS server using python3:

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

from ftplib import FTP_TLS

host='my.url'
user='myuser'
passwd='password_containing_§'

ftp=FTP_TLS(url)
ftp.set_debuglevel(2)
ftp.login(user,passwd)

But this gives an error whenever there is a § in the password.

*cmd* 'PASS ****************************************************************************************************'
*put* 'PASS ****************************************************************************************************\r\n'
*get* '530 Login incorrect.\n'
*resp* '530 Login incorrect.'

Tested with Python 3.5.2 and 3.7.6, same issue.

The exact same code and data works well with Python2.7 (I just change the shebang to python2). Same login data also works well from Filezilla.

Can anybody help out ?


I read this similar question about sending the commands individually, but ftp.sendcmd(...) returns:

ftplib.error_perm: 550 SSL/TLS required on the control channel
pLumo
  • 397
  • 1
  • 13
  • Try adding `from __future__ import unicode_literals` at the top of your Python 2.7 code. In contrast with Python 2.7, Python 3 treats the string as unicode, so it may be it. – agastalver Jul 21 '20 at 13:24
  • Then I get `UnicodeEncodeError: 'ascii' codec can't encode character u'\xa7' in position 9`. – pLumo Jul 21 '20 at 13:28
  • You can learn about this porting on [the official python page](https://docs.python.org/3.3/howto/pyporting.html#bytes-string-literals). You can treat the strings on Python 3 as in Python 2.7, by writing a `b` at the beggining of the literal: `passwd=b'mypasswd'` – agastalver Jul 21 '20 at 13:32
  • But this gives `SyntaxError: bytes can only contain ASCII literal characters.` – pLumo Jul 21 '20 at 13:33
  • I think you can work from here. Try this [answer](https://stackoverflow.com/questions/20485845/how-to-declare-a-byte-array-contains-non-ascii-characters-without-escape-in-pyth). – agastalver Jul 21 '20 at 13:35
  • Thanks for your help and the link, but it doesn't help. With `bytesarray()` I get Login incorrect, using `bytes()` I get `TypeError: Can't convert 'bytes' object to str implicitly` and if casting with `str()` I get again Login incorrect :-( – pLumo Jul 21 '20 at 13:47
  • @agastalver I think you're on the wrong track here anyways as this is not a bytestring, it's a simple unicode string and I still don't get what is wrong . – pLumo Jul 22 '20 at 11:30
  • I was wrong too^^ it is a utf-8 string and I need to decode to unicode. See my answer. Thanks a lot for your help, sent me in the right direction ! – pLumo Jul 23 '20 at 07:17

1 Answers1

0

This was the solution:

passwd = passwd.encode('utf-8').decode('unicode-escape')

(via)

pLumo
  • 397
  • 1
  • 13