socket.gaierror:[Errno 8]提供nodename或servname,或者不知道

时间:2015-08-17 09:10:40

标签: python-2.7 ftp ftplib configparser

我正在尝试使用运行OS X Yosemite(10.10.5)的Mac上的以下Python脚本连接到我的ftp服务器:

from ftplib import FTP
import ConfigParser

config_file = "ftp.config"
config = ConfigParser.ConfigParser()
try:
    config.read(config_file)
except:
    raise Exception("Failed reading {} file.").format(config_file)

server_address  = config.get("main", "server")
user_name = config.get("main", "user_name")
password  = config.get("main", "password")

ftp = FTP(server_address)
ftp.login(user_name, password)
print "Logging in as '{}'".format(user_name)

该脚本从文件“ftp.config”获取服务器信息,其中包含以下内容:

[main]
enable      =   1
server      =   "my.ftp.address"
user_name   =   "some_user_name"
password    =   "some_password"

当我运行脚本时,出现以下错误:

Traceback (most recent call last):  
    File "ftp2.py", line 34, in <module>
        ftp = FTP(server_address)
    File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ftplib.py", line 120, in __init__
        self.connect(host)
    File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ftplib.py", line 135, in connect
    self.sock = socket.create_connection((self.host, self.port), self.timeout)
    File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 557, in create_connection
        for res in getaddrinfo(host, port, 0, SOCK_STREAM): 
    socket.gaierror: [Errno 8] nodename nor servname provided, or not known

我不知道为什么会发生错误。我尝试.split()来自“.config”文件的字符串,以便删除多余的行,但这似乎没有帮助。

任何想法,伙计们?

感谢。

1 个答案:

答案 0 :(得分:3)

删除配置文件中的引号

[main]
enable      =   1
server      =   my.ftp.address
user_name   =   some_user_name
password    =   some_password