[Twisted-Python] Multiple clients

Szabolcs Balogh bszabolcs at gmail.com
Fri Jun 18 10:34:08 EDT 2010

I have used the following code, which is working until I called it multiple 

class ImapProtocol(twisted.mail.imap4.IMAP4Client):


class ImapFactory(twisted.internet.protocol.ClientFactory):

    def __init__(self, account):

        self.account = account
        self.deferred = twisted.internet.defer.Deferred()
    self.protocol = ImapProtocol

    def clientConnectionFailed(self, connection, reason):
        Called when the client has failed to connect


class ImapClient(gobject.GObject):

    def __init__(self, account):

        super(ImapClient, self).__init__()

        self.account = account
        self.host = self.account.get_server_address()
        self.port = self.account.get_server_port()
        self.ssl = self.account.get_use_ssl()

    def got_mailbox_list(self, result):


    def handle_error(self, error):


    def get_mailbox_list(self):
        Get the list of existing mailboxes.

        imapfactory = ImapFactory(self.account, self.LIST_MAILBOX)
        if self.ssl:
            # Using SSL for encrypted connections
            contextfactory = twisted.internet.ssl.ClientContextFactory()
            twisted.internet.reactor.connectSSL(self.host, self.port, 
                                        imapfactory,  contextfactory, 4)
            # Server doesn't require SSL
            twisted.internet.reactor.connectTCP(self.host, self.port, 
                                                imapfactory, 4)


I call it in the following way:

        imap_client = imapclient.ImapClient(account)

If I call it only once, it executes successfully, but if I call it twice it 
get blocks and never finishes...

So how can I start the reactor and use the connectSSL/connectTCP methods 
multiple times? How to add new clients to an existing reactor?

On Friday 18 June 2010 16:01:02 exarkun at twistedmatrix.com wrote:
> On 10:36 am, bszabolcs at gmail.com wrote:
> >Hy,
> >
> >Can somebody send me a small example how to use multiple clients at
> >same
> >time? For example to connect to 3 different IMAP server. I need this
> >because
> >I'm working on an email client engine. I can connect to POP/IMAP/SMTP
> >server
> >to download and send messages, but I have created those methods
> >separated,
> >and each time when I check for new messages or send a message I use
> >reactor.run() and reactor.stop(). But calling it multiple times is a
> >problem.
> Why?
> >So I need something:
> >- when I press button1, the application checks for new messages on POP
> >server.
> >- when I press button2, the application checks for new messages on IMAP
> >server.
> How about:
>   def onbutton1(self):
>       checkPOP()
>   def onbutton2(self):
>       checkIMAP()
> >I know how to check for messages on IMAP/POP server, but I don't know
> >how to
> >do it periodically with twisted...
> Periodically as in "once every N seconds"?  Check out reactor.callLater
> or twisted.internet.task.LoopingCall.
> Jean-Paul
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python

More information about the Twisted-Python mailing list