【问题标题】:upload and downloading to ftp using python使用python上传和下载到ftp
【发布时间】:2021-12-08 23:29:13
【问题描述】:

首先是一个学习python的新手。我正在尝试使用 python 将文件上传/下载到服务器。服务器 ftp 没有域名,只有一个 IP 地址,即 192.168.0.20 和端口 21 我尝试了以下代码,但它给出了很多错误

import ftplib

#FTP server credentials
FTP_HOST = '192.16.0.20'
PORT='21'
FTP_USER = 'ftpuser'
FTP_PASS = 'ftpuser'

# connecting to the FTP server
ftp = ftplib.FTP(FTP_HOST,PORT, FTP_USER, FTP_PASS)
# force UTF-8 encoding
ftp.encoding = "utf-8"
# local file name you want to upload
filename = "test.txt"
with open(filename, "rb") as file:
# use FTP's STOR command to upload the file
    ftp.storbinary(f'STOR {filename}', file)
# list current files & directories
ftp.dir()
# quit and close the connection
ftp.quit()

求助-我哪里出错了

【问题讨论】:

  • “它给出了很多错误” 并没有向我们提供有关您遇到的错误的任何信息。我们需要您得到准确的错误信息。
  • 你能指定这些错误的样子吗?
  • 文件“C:\Users\Josiah\AppData\Local\Programs\Python\Python39\lib\ftplib.py”,第 252 行,在 getresp 中引发 error_perm(resp) ftplib.error_perm: 530 用户无法登录。

标签: python ftp


【解决方案1】:

在创建FTP 对象后,您可能需要使用ftp.login() 登录到服务器。

你得到了哪些确切的错误?

【讨论】:

  • 如何在我的代码中执行此操作?请举个例子
  • OP 已经通过ftplib.FTP(FTP_HOST,PORT, FTP_USER, FTP_PASS)登录
  • documetation 中的示例需要额外的登录调用。
  • 通过将凭据传递给构造函数或使用login 方法登录都没有关系。都是一样的。
  • 啊,是的,你是对的。我没有检查该构造函数的文档。 @OP 你得到了哪些确切的错误?
猜你喜欢
  • 2011-10-08
  • 2011-08-14
  • 1970-01-01
  • 2013-10-08
  • 1970-01-01
  • 1970-01-01
  • 2011-06-01
  • 1970-01-01
相关资源
最近更新 更多