Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
902 views
in Technique[技术] by (71.8m points)

sockets - Python 3 ftplib error "Name or service not known"

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

Here is the relevant code-

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. I have the correct URL and credentials
  2. I am behind a proxy server
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

This is correct:

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

The full path goes to an argument of the RETR command:

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...