Python 3 ftplib 错误“名称或服务未知"

Python 3 ftplib error quot;Name or service not knownquot;(Python 3 ftplib 错误“名称或服务未知)
本文介绍了Python 3 ftplib 错误“名称或服务未知"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试使用 Python 3 的 ftplib 库从 FTP 服务器下载文件.

I am trying to download a file from FTP server using ftplib library of Python 3.

这里是相关代码-

ftp = ftplib.FTP("ftp://library.daisy.org:21/User_****/Wise & Otherwise-22.zip") 
ftp.login("xxxxx", "xxxxxxx") 
ftp.cwd(path)
ftp.retrbinary("RETR " + filename, open(filename, 'wb').write)
ftp.quit()

当我尝试运行脚本时,会显示以下错误-

When I try to run the script the following error shows up-

Traceback (most recent call last):
  File "reader.py", line 604, in <module>
    sp.process_user_choice()
  File "reader.py", line 72, in process_user_choice
    self.download_books()   File "reader.py", line 324, in download_books
    ftp = ftplib.FTP(all_urls[response]) 
  File "/usr/lib/python3.5/ftplib.py", line 118, in __init__
    self.connect(host)
  File "/usr/lib/python3.5/ftplib.py", line 153, in connect
    source_address=self.source_address)
  File "/usr/lib/python3.5/socket.py", line 694, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "/usr/lib/python3.5/socket.py", line 733, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known

<小时>

一些笔记-


Some notes-

  1. 我有正确的 URL 和凭据
  2. 我在代理服务器后面

推荐答案

FTP 构造函数的 host 参数,顾名思义,只接受一个主机名,例如 <代码>library.daisy.org.你传入的是一个完整的 URL.

The host argument of FTP constructor takes, as the name suggests, a hostname only, like library.daisy.org. You are passing in a whole URL.

这是正确的:

ftp = ftplib.FTP("library.daisy.org")

<小时>

完整路径转到 RETR 命令的参数:

ftp.retrbinary("RETR User_****/Wise & Otherwise-22.zip", open(filename, 'wb').write)

<小时>

当您通过代理连接时,您也必须满足这一点.


As you are connecting via proxy, you have to cater for that too.

这里有很多关于这部分的问题.但是您没有告诉我们您使用的是哪种代理,所以我不能更具体.

There are lot of questions here covering that part. But you didn't tell us what kind of proxy you are using, so I cannot be more specific.

这篇关于Python 3 ftplib 错误“名称或服务未知"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

patching a class yields quot;AttributeError: Mock object has no attributequot; when accessing instance attributes(修补类会产生“AttributeError:Mock object has no attribute;访问实例属性时)
How to mock lt;ModelClassgt;.query.filter_by() in Flask-SqlAlchemy(如何在 Flask-SqlAlchemy 中模拟 lt;ModelClassgt;.query.filter_by())
FTPLIB error socket.gaierror: [Errno 8] nodename nor servname provided, or not known(FTPLIB 错误 socket.gaierror: [Errno 8] nodename nor servname provided, or not known)
Weird numpy.sum behavior when adding zeros(添加零时奇怪的 numpy.sum 行为)
Why does the #39;int#39; object is not callable error occur when using the sum() function?(为什么在使用 sum() 函数时会出现 int object is not callable 错误?)
How to sum in pandas by unique index in several columns?(如何通过几列中的唯一索引对 pandas 求和?)