The first argument to the FTP()
constructor must be a hostname, not a URL:
f=ftplib.FTP('ftp.ncbi.nih.gov')
f.cwd('/genomes/Bacteria/')
(You probably need to login or provide a username/password in the FTP()
call before calling cwd
)
题
When I run this code from iPython 0.13:
import ftplib
f=ftplib.FTP('ftp://ftp.ncbi.nih.gov/genomes/Bacteria/')
I get the following error: gaierror: [Errno 8] nodename nor servname provided, or not known
The full error is below. I'm running python 2.7.1 on Mac OS X v10.7.5 (Lion). I've done some searching and it appears that in other situations similar error was generated. The strange part is I can do
import urllib2
tt= urllib2.urlopen('ftp://ftp.ncbi.nih.gov/genomes/Bacteria/')
and it works but if I could used the FTP functions I could avoid the parsing of the urlopen since my final goal is select a subset of directories and download their content.
Any idea how this error can be solved or bypassed? Thanks in Advance
---------------------------------------------------------------------------
gaierror Traceback (most recent call last)
<ipython-input-2-91f3bda2d528> in <module>()
----> 1 f=ftplib.FTP('ftp://ftp.ncbi.nih.gov/genomes/Bacteria/')
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ftplib.pyc in __init__(self, host, user, passwd, acct, timeout)
115 self.timeout = timeout
116 if host:
--> 117 self.connect(host)
118 if user:
119 self.login(user, passwd, acct)
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ftplib.pyc in connect(self, host, port, timeout)
130 if timeout != -999:
131 self.timeout = timeout
--> 132 self.sock = socket.create_connection((self.host, self.port), self.timeout)
133 self.af = self.sock.family
134 self.file = self.sock.makefile('rb')
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.pyc in create_connection(address, timeout, source_address)
551 host, port = address
552 err = None
--> 553 for res in getaddrinfo(host, port, 0, SOCK_STREAM):
554 af, socktype, proto, canonname, sa = res
555 sock = None
gaierror: [Errno 8] nodename nor servname provided, or not known
解决方案
The first argument to the FTP()
constructor must be a hostname, not a URL:
f=ftplib.FTP('ftp.ncbi.nih.gov')
f.cwd('/genomes/Bacteria/')
(You probably need to login or provide a username/password in the FTP()
call before calling cwd
)