Search code examples
pythonftpftplib

Python's FTP library connects to an IP but not its domain


I'm using ftplib's FTP class to connect to ftp://emi.nasdaq.com. Using the domain doesn't work, but using FileZilla I can see the IP address the domain name resolves to, and the IP address works.

def main():
print("Grabbing data...")
with FTP() as ftp:
    ftp.connect('ftp://emi.nasdaq.com', 21)

Specifically, the domain throws a GAIError ([Errno 11001] getaddrinfo failed). I'm using Python 3.8. How can I resolve the domain to its IP before passing it to the FTP class?


Solution

  • ftplib docs suggest to use address without leading ftp:// I did

    from ftplib import FTP
    ftp = FTP()
    ftp.connect('emi.nasdaq.com',21)
    ftp.login()
    ftp.retrlines('LIST')
    ftp.quit()
    

    and output was

    07-27-13  04:30PM       <DIR>          aspnet_client
    03-21-13  09:31AM       <DIR>          Baltic
    07-21-21  02:42PM       <DIR>          Basic_NLS
    05-21-19  08:29AM       <DIR>          Index
    07-21-21  02:44PM       <DIR>          ITCH
    08-21-20  02:07PM       <DIR>          Nasdaq Canada
    08-05-19  09:31AM       <DIR>          Nordic
    07-19-21  10:11AM       <DIR>          Options
    12-01-20  09:49AM       <DIR>          Test
    07-27-21  10:26AM       <DIR>          Web Based Reports
    09-09-21  05:38PM                  168 web.config