I have a function that tries some list of params to connect to ftp and connects to the first server that it could.

```
def connect(params):
    for user, passw, host in params:
        try:
            import pdb;pdb.set_trace()
            with FTPHost(host, user, passw) as h:
                return h
        except FTPError as e:
            logger.debug("Can't connect to ftp error is {}".format(e))
    else:
        raise Exception(
            "Can't connect to ftp server with none of the {}".format(params)
        )

```

Further in code I'm trying something like that

```
host = connect(*args)
host.walk()

```

But right after return the connection is closed. This is probably how it should work, I guess, although I hoped that it wouldn't.
But now I don't really know how to encapsulate trials of connection from application logic properly.

I mean certainly I could change this into continuation passing style (this is correct name for that, right?)

```
def connect(params, some_application_function):
    for user, passw, host in params:
        try:
            import pdb;pdb.set_trace()
            with FTPHost(host, user, passw) as host:
                some_application_function(host)
        except FTPError as e:
            logger.debug("Can't connect to ftp error is {}".format(e))
    else:
        raise Exception(
            "Can't connect to ftp server with none of the {}".format(params)
        )

```

But this seems less readable. Are there any other options?