2013-04-04 98 views
1

當我運行從IPython的0.13以下代碼:的Python FTPLIB問題

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

我得到以下錯誤:gaierror:[錯誤8]的節點名也不servname提供,或者不知道

完整的錯誤被下面。 我在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 

回答

8

的第一個參數FTP()構造函數必須是主機名,而不是一個URL:

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

(你可能需要登錄或FTP()呼叫提供用戶名/密碼致電cwd

+1

謝謝!這樣可行。我只需要添加 f.login() 以匿名身份登錄! – JAC 2013-04-04 13:01:30