将文件上载到 FTP 服务器时出错



我有一个程序,它将文本文件发送到ftp服务器。代码为:

def sendBug():
    session = ftplib.FTP('ftp://xxxx.xxxx.xxxx.xxxx', " ", " ")
    bugfile = open(bugreport.txt, "r")
    session.storlines("STOR bugreport.txt", bugfile)
    bugfile.close()
    session.quit()

但是,当我运行该脚本时,我得到如下内容:

Traceback (most recent call last):
File "C:Python27liblib-tkTkinter.py", line 1470, in __call__
return self.func(*args)
 File "C:UsersNitroDesktoppy2exe stuffschool firewall ui.py", line 46, in getnews
ftp= ftplib.FTP(server)
File "C:Python27libftplib.py", line 120, in __init__
self.connect(host)
File "C:Python27libftplib.py", line 135, in connect
self.sock = socket.create_connection((self.host, self.port), self.timeout)
File "C:Python27libsocket.py", line 553, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
gaierror: [Errno 11004] getaddrinfo failed

有什么方法可以解决这个问题吗?我使用 IIS 托管 ftp 服务器。学校防火墙UI是我的程序。

对于第一个服务器行不需要"ftp://"

基本上gaierror: [Errno 11004] getaddrinfo failed意味着ftp模块无法理解您给他的'ftp://xxxx.xxxx.xxxx.xxxx'地址 - 主机名。

来自文档的gaierror

对于与地址相关的错误,getaddrinfo() 和 getnameinfo() 会引发此异常。随附的值是一对(错误、字符串),表示库调用返回的错误。string 表示错误的说明,由 gai_strerror() C 函数返回。错误值将匹配此模块中定义的 EAI_* 常量之一。

您正在传递带有主机,用户名和密码的ftplib.FTP()(用户和密码都是您的案例中的空格)

如果它应该是匿名的,只需使用:

ftp = ftplib.FTP('you address')     
ftp.login() 

如果它仍然不起作用,请尝试ping您的地址或尝试将一些已知的FTP连接到低调的问题所在。

你也可以试试这个链接,它非常有用

有关python FTP的更多信息,请尝试文档

最新更新