1

当我从 iPython 0.13 运行这段代码时:

import ftplib
f=ftplib.FTP('ftp://ftp.ncbi.nih.gov/genomes/Bacteria/')

我收到以下错误:gaierror: [Errno 8] nodename nor servname provided, or not known

完整的错误如下。我在 Mac OS X v10.7.5 (Lion) 上运行 python 2.7.1。我做了一些搜索,似乎在其他情况下也产生了类似的错误。奇怪的是我能做到

import urllib2
tt= urllib2.urlopen('ftp://ftp.ncbi.nih.gov/genomes/Bacteria/')

它可以工作,但如果我可以使用 FTP 功能,我可以避免解析 urlopen,因为我的最终目标是选择一个目录子集并下载它们的内容。

知道如何解决或绕过此错误吗?提前致谢

---------------------------------------------------------------------------
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
4

1 回答 1

10

构造函数的第一个参数FTP()必须是主机名,而不是 URL:

f=ftplib.FTP('ftp.ncbi.nih.gov')
f.cwd('/genomes/Bacteria/')

(您可能需要在FTP()调用之前登录或在调用中提供用户名/密码cwd

于 2013-04-04T12:51:22.020 回答