Search code examples
pythoneventstwistedreactor

Accessing all objects created from ClientFactory in Twisted?


I'm working on a basic Twisted application to help me learn how reactors work with multiple services. The basic outline of what I'd like my script to do is as follows:

My script will be both a Web Server and an IRC Client. Every time a request is made to the web server, the script should say a message on IRC.

I've got an IRC client working, and a twisted.web server working, and can have them run simultaneously in one script. The problem occurs when I try to make them interact with one another. Here's how I initiate the server/client:

import sys
from twisted.words.protocols import irc
from twisted.web import server, resource
from twisted.internet import protocol, reactor

# Define my custom IRC Client, ClientFactory, and Web Application

chan = sys.argv[1]
site = server.Site(Home())
reactor.listenTCP(8080, site)
reactor.connectTCP('irc.freenote.net', 6667, IRCBotFactory(chan))
reactor.run()

Using the code above, the two parts of my client run simultaneously without issue. When trying to make the IRC Client send messages to the server upon HTTP request, however, I realized that I don't actually have reference to an IRCBot intance, as I initiated the reactor with the IRCBotFactory and let Twisted handle initialization of the actual bot object.

Is there a way to get all child instances of a factory in Twisted, or is there another way for me to initiate the IRC Client (perhaps bypassing the Factory and simply using an IRCBot instance)?


Solution

  • One of the Twisted FAQ entries entries discusses a problem like this one. Just remember that a Site is a factory and it should be easy to apply a similar solution to your case.