Search code examples
pythonftpftplibpyftpdlib

Cant connect to local pyftpdlib FTP server: [WinError 10061] No connection could be made


I am trying to upload/download file to local FTP server, but it gives me the error mentioned in title. For server I am using pyftpdlib:

import os

from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer

# instantiate a dummy authorizer 
authorizer = DummyAuthorizer()

# instantiate anonymous user to current directory
authorizer.add_anonymous(os.getcwd())

# FTP handler class
handler = FTPHandler
handler.authorizer = authorizer

# setup server on localhost, port = 21
address = ('', 21)
server = FTPServer(address, handler)

# set a limit for connections
server.max_cons = 10
server.max_cons_per_ip = 3

# start ftp server
server.serve_forever()

Here is client code:

from ftplib import FTP

# connect to FTP server
client = FTP(host="127.0.0.1")
client.login()

# list the contents of directory
client.retrlines('LIST')

But the FTP constructor throws:

ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it.

Initially I thought it was port issue (I did not specify port from client when connected), so changed server port to 21, which I believe is default port.

When running server code, I get firewall alert, but when I give it the permission it runs normally. How can I connect to server from the client side?


Solution

  • I'm not sure what '' as an address would do on the server side. Either it is a wrong value in the first place. Or it may resolve to a different IP address than the 127.0.0.1. You should use the same value both on server and client side.

    I'd start with 127.0.0.1 on the server side.

    address = ('127.0.0.1', 21)