使用 ftplib 访问 FTP URL

2022-01-09 00:00:00 python python-3.x ftp ftplib

问题描述

我在 Windows 中使用带有 ftplib 的 python 来访问 ftp5.xyz.eu 的文件夹.

I am using python in Windows with ftplib to access a folder at ftp5.xyz.eu.

文件夹是 ftp5.xyz.eu 中的文件夹 'foo bar' 中的 'baz'.所以:ftp5.xyz.eu/foo bar/baz

The folder is 'baz' in ftp5.xyz.eu in the folder 'foo bar'. So : ftp5.xyz.eu/foo bar/baz

我在 ftp5.xyz.eu 成功连接,但是当我将整个路径写入文件夹时,它给了我一个错误:

I connect successfully at ftp5.xyz.eu but when i write the whole path to the folder it gives me an error:

from ftplib import FTP

#domain name or server ip:
ftp = FTP('ftp5.xyz.eu/foo%20bar')
...
ftp.dir()

错误如下:

  File "C:Usersmirel.voicuAppDataLocalProgramsPythonPython37libftplib.py", line 117, in __init__
    self.connect(host)
  File "C:Usersmirel.voicuAppDataLocalProgramsPythonPython37libftplib.py", line 152, in connect
    source_address=self.source_address)
  File "C:Usersmirel.voicuAppDataLocalProgramsPythonPython37libsocket.py", line 707, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "C:Usersmirel.voicuAppDataLocalProgramsPythonPython37libsocket.py", line 748, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed


解决方案

这个和空间无关.FTP 构造函数的第一个参数 是 host - 主机名 或 IP 地址 - 不是 URL.

This has nothing to do with the space. The first argument of FTP constructor is host – a hostname or an IP address – not an URL.

应该是这样的:

ftp = FTP('ftp5.xyz.eu')


如果您想列出 foo bar 子文件夹中的文件,请执行以下操作:


If you want to list files in foo bar subfolder, either do:

ftp.cwd('foo bar')      
ftp.dir()

ftp.dir('foo bar')

相关文章