2

I have a local FTP server set up on my machine. It's set up on 127.0.0.1 and port 21. When I'm trying to access it using the python-based library ftplib, the program is throwing an error. This is the code that I'm running.

from ftplib import FTP

ftp = FTP("ftp://127.0.0.1", user = "VAIBHAV", passwd = "12345")
ftp.dir()
ftp.retrlines("LIST")
ftp.quit()

And, this is the error.

Traceback (most recent call last):
  File ".\main.py", line 5, in <module>
    ftp = FTP("ftp://127.0.0.1", user = "VAIBHAV", passwd = "12345")
  File "C:\Users\VAIBHAV\AppData\Local\Programs\Python\Python38\lib\ftplib.py", line 117, in __init__
    self.connect(host)
  File "C:\Users\VAIBHAV\AppData\Local\Programs\Python\Python38\lib\ftplib.py", line 152, in connect
    self.sock = socket.create_connection((self.host, self.port), self.timeout,
  File "C:\Users\VAIBHAV\AppData\Local\Programs\Python\Python38\lib\socket.py", line 787, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "C:\Users\VAIBHAV\AppData\Local\Programs\Python\Python38\lib\socket.py", line 918, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed

How should I fix this error?

I'm using python 3.8.7 and I have Windows 10 on my machine.

Vaibhav
  • 562
  • 5
  • 12

1 Answers1

3

Looking at the documentation, I don't see any "ftp://" before the hostname, so I think if you change it to

ftp = FTP("127.0.0.1", user="VAIBHAV", passwd="12345")
# or try "localhost" as well
ftp = FTP("localhost", user="VAIBHAV", passwd="12345")

it should work, or at least give you a different error.

Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
  • If you're getting these urls from somewhere outside your control, you can remove the `ftp://` part like this: `from urllib.parse import urlparse; urlparse("ftp://127.0.0.1").hostname` – Boris Verkhovskiy Feb 23 '21 at 13:15
  • Got it. It's like filtering the protocol name and other parts and just retrieving the domain name. – Vaibhav Feb 23 '21 at 13:34