Python and ftplib, unable to login?

2019-08-06 10:07发布

问题:

I'm learning Python and I tried using the FTPLib module for Python with this code:

import ftplib

connect = ftplib.FTP('ftp://www.website.com')
connect.login = ('username', 'password')
data = []
connect.dir(data.append)
connect.quit()
for line in data:
    print line

(I'm aware that the website, username and password is incorrect, I used my website data which I don't want to share) I received the following error after running the code:

 Traceback (most recent call last):
  File "ftp.py", line 3, in <module>
    ftp = FTP('ftp://www.website.com')   # connect to host, default port
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ftplib.py", line 120, in __init__
    self.connect(host)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ftplib.py", line 135, in connect
    self.sock = socket.create_connection((self.host, self.port), self.timeout)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 553, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno 8] nodename nor servname provided, or not known

Just to clarify, I'm using Python 2.7 on a Mac. I don't think there is anymore details I could share. Thank you for your help!

回答1:

Thanks Joel Hinz, I just needed to remove 'ftp://' from my hostname. Thanks!