[Twisted-Python] Where is best place to put my custom code?

Jp Calderone exarkun at intarweb.us
Thu Jan 1 01:38:10 EST 2004

On Wed, Dec 31, 2003 at 03:58:50PM -0800, JD wrote:
> Hi,
> I'm open for suggestions on where I can put my customized code.    I seem
> to have a choice between putting it in the "factory" or in the "protocol",
> and I cannot seem to decide where the best place would be.
> My application is simple.....   here is all it should do....
> 1) establish a connection to an IRC server.
> 2) send an IRC command,  like a "who" command,  and extract the info
> 3) then exit the client
> I could put my code in the IRCClient's "connectionMade" method.    Or I
> could put it in the factory's "startedConnecting" method.  The code would
> be simple, issue a "who" command, and take it's output and return it.  So,
> in my connectionMade method I would issue the command...  then call
> reactor.stop() which would exit from my reactor.run().

  startedConnecting isn't the right place.  connectionMade is a little
closer, but since you need to log in (tell the IRC server your nickname,
etc) before you can issue commands like WHO, it isn't totally appropriate
either.  signedOn is the best place, since it is called by IRCClient once
the server acknowledges the login information.

> I think the factory's "startedConnecting" might get called first,  then
> somewhere down in this mess, the Client would call me back with the
> "connectionMade", and I could do it there. I just need to know the
> appropriate place to put my code.  None of these examples in the
> 'examples' directories make this clear.
> My next task is to know how to extract the 'who' data back from my initial
> "who"

  Indeed.  This is not very easy to do with IRCClient, since the response
the server sends doesn't have a whole lot to associate it with the WHO
command you originally issued.  You need to keep track of outstanding
commands and try to match them up with server replies.

  I recognized this deficiency some time ago and did some initial work
towards improving the API.  WHO is one of the commands I did manage to
improve, but I never moved any of the work out of my sandbox.  You can find
it in Twisted/sandbox/exarkun/irc2.py.  It is under the same license as the
rest of Twisted, even though it currently lacks any indication fo that.  If
it would be helpful to you, please feel free to use it.

  The improvements take one general form: commands which will produce server
responses are exposed as functions which return a Deferred.  When the server
replies, the parsed information will be passed to that Deferred's callback.

  Here is a simple example of how you might use it (untested):

    class WhoingIRCClient(irc2.AdvancedClient):
        def signedOn(self):
            # Find everyone IRC'ing from Twisted Matrix's server.
                ).addCallbacks(self._cbWho, self._ebWho

        def _cbWho(self, results):
            print 'channel user host server nick flags hops realname'
            for r in results:
                print ', '.join(r)

        def _ebWho(self, failure)
            print 'Error in who!'

        def _shutdownEverything(self, ignored):

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://twistedmatrix.com/pipermail/twisted-python/attachments/20040101/279f3426/attachment.pgp 

More information about the Twisted-Python mailing list