本文介绍了使用ftplib访问FTP URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Windows中将python和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:\Users\mirel.voicu\AppData\Local\Programs\Python\Python37\lib\ftplib.py", line 117, in __init__
    self.connect(host)
  File "C:\Users\mirel.voicu\AppData\Local\Programs\Python\Python37\lib\ftplib.py", line 152, in connect
    source_address=self.source_address)
  File "C:\Users\mirel.voicu\AppData\Local\Programs\Python\Python37\lib\socket.py", line 707, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "C:\Users\mirel.voicu\AppData\Local\Programs\Python\Python37\lib\socket.py", line 748, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed

推荐答案

这与空格无关. FTP 构造函数是 host –一个 hostname 或一个 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')

这篇关于使用ftplib访问FTP URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!