473,407 Members | 2,320 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Twisted: How to sendLine from outside the LineReceiver class?

Qp
Hello. I'm building a simple chat server and client interface, and I've got
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 class)
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. How
do I do this?

The code I am using for the classes is as follows. The error comes when I
call chatFactory.sendMessage():

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 insert
app.text_output.insert(END, line +"\n")
app.text_output.config(state=DISABLED)
app.text_input.delete(0, END) #clear text_input
app.text_output.see(END) #move scrollbar to the bottom

def connectionLost(self, reason):
reactor.stop()

class chatFactory(protocol.ClientFactory):
protocol = chatClient

def clientConnectionFailed(self, connector, reason):
reactor.stop()

def clientConnectionLost(self, connector, reason):
reactor.stop()

def sendMessage(self):
c = chatClient()
c.sendLine("Hey there")
<<<

Jul 18 '05 #1
2 6208
[You'll probably get more answers to Twisted questions on the Twisted
mailing-list: tw************@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 got
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 class)
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. How
do I do this?

The code I am using for the classes is as follows. The error comes when I
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:

http://twistedmatrix.com/documents/howto/clients

and also:

http://twistedmatrix.com/documents/c...tml#connectTCP

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!")
self.factory.clientReady(self)

def lineReceived(self, line):
app.text_output.config(state=NORMAL) #enable text_output for insert
app.text_output.insert(END, line +"\n")
app.text_output.config(state=DISABLED)
app.text_input.delete(0, END) #clear text_input
app.text_output.see(END) #move scrollbar to the bottom

def connectionLost(self, reason):
reactor.stop()

class ChatFactory(protocol.ClientFactory):
protocol = ChatClient

def clientConnectionFailed(self, connector, reason):
reactor.stop()

def clientConnectionLost(self, connector, reason):
reactor.stop()

def startFactory(self):
self.messageQueue = []
self.clientInstance = None

def clientReady(self, instance):
self.clientInstance = instance
for msg in self.messageQueue:
self.sendMessage(msg)

def sendMessage(self, msg='Hey there'):
if self.clientInstance is not None:
self.clientInstance.sendLine(msg)
else:
self.messageQueue.append(msg)
----

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 if
called too soon. The queue avoids that problem.

A better solution might be to not write the factory at all, and use
twisted.internet.protocol.clientCreator:

http://twistedmatrix.com/documents/c...ntCreator.html

----
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')
deferred.addCallback(connectionReady)

# Log any errors that occur (such as connection failed)
deferred.addErrback(log.err)
----

-Andrew.
Jul 18 '05 #2
Qp
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...

Thanks again,
QP

"Andrew Bennetts" <an***************@puzzling.org> wrote in message
news:ma************************************@python .org...
[You'll probably get more answers to Twisted questions on the Twisted
mailing-list: tw************@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 got 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 class) 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. How do I do this?

The code I am using for the classes is as follows. The error comes when I 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:

http://twistedmatrix.com/documents/howto/clients

and also:

http://twistedmatrix.com/documents/c...t.interfaces.I
ReactorTCP.html#connectTCP
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!")
self.factory.clientReady(self)

def lineReceived(self, line):
app.text_output.config(state=NORMAL) #enable text_output for insert app.text_output.insert(END, line +"\n")
app.text_output.config(state=DISABLED)
app.text_input.delete(0, END) #clear text_input
app.text_output.see(END) #move scrollbar to the bottom

def connectionLost(self, reason):
reactor.stop()

class ChatFactory(protocol.ClientFactory):
protocol = ChatClient

def clientConnectionFailed(self, connector, reason):
reactor.stop()

def clientConnectionLost(self, connector, reason):
reactor.stop()

def startFactory(self):
self.messageQueue = []
self.clientInstance = None

def clientReady(self, instance):
self.clientInstance = instance
for msg in self.messageQueue:
self.sendMessage(msg)

def sendMessage(self, msg='Hey there'):
if self.clientInstance is not None:
self.clientInstance.sendLine(msg)
else:
self.messageQueue.append(msg)
----

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 if called too soon. The queue avoids that problem.

A better solution might be to not write the factory at all, and use
twisted.internet.protocol.clientCreator:

http://twistedmatrix.com/documents/c...t.protocol.Cli
entCreator.html
----
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')
deferred.addCallback(connectionReady)

# Log any errors that occur (such as connection failed)
deferred.addErrback(log.err)
----

-Andrew.


Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Alexander Eberts | last post by:
I'm new to python so appologies to the group if this question is asked often. I'm wondering if it's possible to query an object instance to find out what arguments the instance's __init__ function...
1
by: Keith Rebello | last post by:
I have written a class to draw dimension lines (like you see in architectural drawings). I intend to use this class in engineering programs that have graphical output. The class has various...
2
by: ThunderMusic | last post by:
Hi, I'm building a class library and I use some classes in my library that I would not like to expose when using the class library. I explain : I use let's say Class1, Class2 and Class3. In...
3
by: freak | last post by:
Using PHP version: 4.3.7 I try to define my db vars outside the class! Why this will not work? class mytest{ //***************** set db settings *************************** var $HOST ; var...
4
by: gopal | last post by:
I have the following class template declaration //class template declaration template <class T> class A { public: T t1; A(T); }; I would like to define the class A constrcutor outside. Any...
2
by: KimRaver | last post by:
How do i declare a method that returns a doublepointer to a subclass, outside the Class-definition ? That is, as of now I have Class House { ... ... Class Room {
1
by: =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki | last post by:
We have here class of C++ language: namespace Apt { class Name1 { public: class Name2; }; } I 'm give ebove class Name2 so a declalation of class without a definition
1
SamKL
by: SamKL | last post by:
Hey, I'm no expert on PHP, and I have somewhat of an understanding of object oriented code. Anyway, getting right to the problem. I'm using PHP4, so base it off of that. Basically I have 2...
2
by: Suraj Kumar | last post by:
why static member is required to redeclare outside the class ?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.