socket.gaierror: [Errno 8] nodename nor servname provided, or not known

St4rb0y picture St4rb0y · Aug 17, 2015 · Viewed 12.3k times · Source

I'm trying to connect to my ftp server with the following Python script on a Mac running OS X Yosemite (10.10.5):

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)

The script gets the server information from the file "ftp.config" with the following content:

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

When I run the script, I get the following error:

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

I have no idea why the error occurs. I've tried to .split() the strings from the ".config" file, in order to remove superfluous lines, but this doesn't seem to help.

Any ideas, guys?

Thanks.

Answer

bleakgadfly picture bleakgadfly · Aug 17, 2015

Remove the quotes in your configuration file

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