Python ftplib 在 FTP 期间失败

Python ftplib fails during FTP

问题:我需要打开从 python 脚本到 android ftp-服务器的 ftp 连接.我收到以下错误:

ConnectionResetError: [Errno 104] Connection reset by peer

错误堆栈跟踪在底部给出。

这是我的 Python 脚本:

import ftplib
try:
      logger.info(ftp_ip+ftp_port)
      FTP=ftplib.FTP()
      ftp_msg=FTP.connect(host=ftp_ip, port=2221)
      logger.info("Message: "+ftp_msg)
      ftp_msg=FTP.login(user=ftp_username, passwd=ftp_pwd)
      logger.info("Message: "+ftp_msg)
      ftp_msg=FTP.pwd()
      logger.info("Current dir: "+ftp_msg)
      filename = '/....xml'
      with open(filename, 'rb') as fp:
       res = FTP.storbinary("STOR " + filename, fp)
except ftplib.all_errors as e:
      logger.error('FTP error:', e) 

我正在为 python FTP 使用 "ftplib" 库。

堆栈跟踪和日志:

2020-05-21 18:53:55,435 192.168.43.1642221
2020-05-21 18:53:55,458 Message: 220 Service ready for new user.
2020-05-21 18:53:55,473 Message: 230 User logged in, proceed.
2020-05-21 18:53:55,479 Current dir: /
Console exception (unhandled most probably):
--- Logging error ---
Traceback (most recent call last):
  File "FTP_Transfer.py", line 98, in ftp_Connect
    res = FTP.storbinary("STOR " + filename, open(filename, 'rb'))
  File "/usr/lib/python3.5/ftplib.py", line 508, in storbinary
    conn.sendall(buf)
ConnectionResetError: [Errno 104] Connection reset by peer

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.5/logging/__init__.py", line 981, in emit
    msg = self.format(record)
  File "/usr/lib/python3.5/logging/__init__.py", line 831, in format
    return fmt.format(record)
  File "/usr/lib/python3.5/logging/__init__.py", line 568, in format
    record.message = record.getMessage()
  File "/usr/lib/python3.5/logging/__init__.py", line 331, in getMessage
    msg = msg % self.args
TypeError: not all arguments converted during string formatting
Call stack:
  File "FTP_Transfer.py", line 120, in <module>
    msg = ftp_Connect()
  File "FTP_Transfer.py", line 103, in ftp_Connect
    logger.error('FTP error:', e)
Message: 'FTP error:'
Arguments: (ConnectionResetError(104, 'Connection reset by peer'),)

所以当它建立连接时,打印当前目录但无法上传文件。 失败的原因是什么?

可能是因为本地目录路径错误。 我用了

 os.chdir("/home/pi/../.../.../")

在 FTP 转移之前,它就像一个魅力。给出 full/absolute 路径不是解决方案。