连接到ftp并下载文件

时间:2018-10-16 19:08:22

标签: python python-3.x ftp ftplib

我正在尝试从ftp下载特定文件,但是尝试连接时出现错误

import ftplib
url = 'ftp://ftp.ensemblgenomes.org/pub/release-41/bacteria//fasta/bacteria_176_collection/_bacillus_aminovorans/cdna/'
ftp = ftplib.FTP(url)

我收到的错误:UnicodeError: encoding with 'idna' codec failed (UnicodeError: label too long)

1 个答案:

答案 0 :(得分:1)

url = 'ftp://ftp.ensemblgenomes.org/pub/release-41/bacteria//fasta/bacteria_176_collection/_bacillus_aminovorans/cdna/'
ftp = ftplib.FTP(url)

来自the documentation of ftplib

  

ftplib.FTP类(host ='',user ='',passwd ='',acct ='',timeout = None,source_address = None)
  返回FTP类的新实例。给定host后,将进行方法调用connect(host)。

换句话说:第一个参数应仅是主机名,而不是带有protocol://host/path的URL。如果要连接到服务器并将服务器更改为特定路径,则需要分步进行:

ftp = ftplib.FTP('ftp.ensemblgenomes.org','ftp','user@example.com')
ftp.cwd('/pub/release-41/bacteria//fasta/bacteria_176_collection/_bacillus_aminovorans/cdna/')