我正在尝试从ftp服务器下载文件,但无法连接。这是代码框架和错误消息。我究竟做错了什么?该网站的用户指南提供了使用您的电子邮件地址作为密码登录为“匿名”的说明。
server = "ftp://ftp.sec.gov/"
directory = "edgar/data/"
filename = "nameoffile.txt"
from ftplib import FTP
ftp = FTP(server, 'anonymous', 'myemail@gmail.com') #Set server address
然后弹出:
Traceback (most recent call last):
File "<pyshell#19>", line 1, in <module>
ftp = FTP(server)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/ftplib.py", line 118, in __init__
self.connect(host)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/ftplib.py", line 153, in connect
source_address=self.source_address)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/socket.py", line 512, in create_connection
raise err
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/socket.py", line 503, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 61] Connection refused
答案 0 :(得分:0)
我没有得到与您相同的错误,但是,FTP不支持URL作为定义远程FTP服务器的方式。相反,它需要主机名或IP地址:
>>> from ftplib import FTP
>>> ftp = FTP('ftp.sec.gov', 'anonymous', 'myemail@gmail.com')
>>> ftp.dir()
-rw-r--r-- 1 root root 369 Nov 2 2009 README.html
-rw-r--r-- 2 root root 749 Aug 15 2000 README.txt
d--x--x--x 2 root wheel 4096 Jul 30 1998 bin
drwxrwxr-x 15 1024 106 4096 Aug 6 02:01 edgar
d--x--x--x 2 root wheel 4096 Aug 14 2000 etc
lrwxrwxrwx 1 root root 33 Nov 16 2012 index.htm -> /usr/local/web/index.nobrowse.htm
lrwxrwxrwx 1 root root 6 Nov 16 2012 pub -> /edgar
-rw-r--r-- 1 root root 86 Sep 3 2010 robots.txt
d--x--x--x 3 root root 4096 Oct 26 2000 usr
drwxr-xr-x 2 root bin 4096 Oct 14 1999 y2kforms