Twisted: How to sendLine from outside the LineReceiver class?
qp at al.net
Mon Mar 1 04:20:59 CET 2004
Thanks a ton! It all makes sense now, and it works in a much better way I
was thinking about (somehow returning a working transport object).
That helps a whole lot...
"Andrew Bennetts" <andrew-pythonlist at puzzling.org> wrote in message
news:mailman.0.1078102318.22701.python-list at python.org...
> [You'll probably get more answers to Twisted questions on the Twisted
> mailing-list: twisted-python at twistedmatrix.com]
> On Sun, Feb 29, 2004 at 02:06:07PM -0500, Qp wrote:
> > Hello. I'm building a simple chat server and client interface, and I've
> > everything working except for this:
> > While the client's basic.LineReceiver protocol class can sendLine when a
> > connection is made (that is, when the class is called automatically),
> > whenever I try to call sendLine from another class (my ClientFactory
> > then I get the following error.
> > "in sendLine
> > return self.transport.write(line + self.delimiter)
> > AttributeError: 'NoneType' object has no attribute 'write'"
> > So, I assume the instance of the LineReceiver is not set up properly.
> > do I do this?
> > The code I am using for the classes is as follows. The error comes when
> > call chatFactory.sendMessage():
> > class chatFactory(protocol.ClientFactory):
> > protocol = chatClient
> > def sendMessage(self):
> > c = chatClient()
> > c.sendLine("Hey there")
> I see you are using ClientFactory, so I presume you want to make a client
> connection to a remote server with this protocol.
> You need to connect your protocol, not just create the object out of thin
> air. See:
> and also:
> I think the easiest way to do what you need is to have the protocol's
> connectionMade signal the factory:
> class ChatClient(basic.LineReceiver):
> def connectionMade(self):
> self.sendLine("A new person has entered the room!")
> def lineReceived(self, line):
> app.text_output.config(state=NORMAL) #enable text_output for
> app.text_output.insert(END, line +"\n")
> app.text_input.delete(0, END) #clear text_input
> app.text_output.see(END) #move scrollbar to the bottom
> def connectionLost(self, reason):
> class ChatFactory(protocol.ClientFactory):
> protocol = ChatClient
> def clientConnectionFailed(self, connector, reason):
> def clientConnectionLost(self, connector, reason):
> def startFactory(self):
> self.messageQueue = 
> self.clientInstance = None
> def clientReady(self, instance):
> self.clientInstance = instance
> for msg in self.messageQueue:
> def sendMessage(self, msg='Hey there'):
> if self.clientInstance is not None:
> Note that I've added a simple message queue because it may take some time
> for the connection to be established, so calls to sendMessage could fail
> called too soon. The queue avoids that problem.
> A better solution might be to not write the factory at all, and use
> from twisted.python import log
> # Create creator and connect
> clientCreator = protocol.ClientCreator(reactor, ChatClient)
> deferred = clientCreator.connectTCP(host, port)
> # When connected, send a line
> def connectionReady(protocol):
> protocol.sendLine('Hey there')
> # Log any errors that occur (such as connection failed)
More information about the Python-list