Bruce Van Horn
Bruce Van Horn

Reputation: 643

Python FTP Server: How do I support both secure and insecure

I can make a basic FTP server that answers port 21 (or in this example 2121) using the sample from the documentation:

from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer
from pyftpdlib.contrib.authorizers import WindowsAuthorizer

def main():
    authorizer = WindowsAuthorizer()
    # Use Guest user with empty password to handle anonymous sessions.
    # Guest user must be enabled first, empty password set and profile
    # directory specified.
    #authorizer = WindowsAuthorizer(anonymous_user="Guest", anonymous_password="")
    handler = FTPHandler
    handler.authorizer = authorizer
    server = FTPServer(('', 2121), handler)
    server.serve_forever()

if __name__ == "__main__":
    main()

Or I can make one that supports TLS (again this is the sample from the docs, except our secure FTP port is 990, not 21 as shown in the original code sample):

from pyftpdlib.servers import FTPServer
from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.contrib.handlers import TLS_FTPHandler


def main():
    authorizer = DummyAuthorizer()
    authorizer.add_user('user', '12345', '.', perm='elradfmw')
    authorizer.add_anonymous('.')
    handler = TLS_FTPHandler
    handler.certfile = 'keycert.pem'
    handler.authorizer = authorizer
    # requires SSL for both control and data channel
    #handler.tls_control_required = True
    #handler.tls_data_required = True
    server = FTPServer(('', 990), handler)
    server.serve_forever()

if __name__ == '__main__':
    main()

Is there a way to make one that answers port 21 (insecure) AND 990 (secured with TLS) in the same script where they share a range of passive ports, for example:

handler.passive_ports = range(50000, 50051)

I imagine I could write two scripts but how will it work if they share the passive port range? That range is a requirement and the current IIS setup we use supports both TLS and insecure connections. I want to use pyftpdlib in a customized server so we can perform custom logic on uploaded files. All that works fine, I just need to understand this last bit and I'm not experienced in writing FTP servers.

Upvotes: 1

Views: 910

Answers (1)

Steffen Ullrich
Steffen Ullrich

Reputation: 123300

There is no need to have both kinds of servers inside the same process but you can simply use separate processes with the same passive_ports setting. When creating a socket it will simply try to use a port from the given range which is not in use by any other process on the system. Thus one server process will just retry with another port if the specific one is already in use by the other server process.

Upvotes: 1

Related Questions