[Twisted-Python] What is wrong with this instance?

As a topic. I got the code: ^^^ form twisted.protocols.telnet import Telnet def keep(): telnet=Telnet() print type(telnet) print getattr(telnet,'write')#all OK until here telnet.write("msg") print "msg" keep() ^^^ and I got TypeError: unbound method write() must be called with Telnet instance as first argument (got str instance instead) I really don't understand where is the mistake.

On Thu, Aug 11, 2005 at 11:01:13AM +0200, Michał Tyde wrote:
As a topic. I got the code: ^^^ form twisted.protocols.telnet import Telnet
def keep(): telnet=Telnet() print type(telnet) print getattr(telnet,'write')#all OK until here telnet.write("msg") print "msg" keep() ^^^
No, you don't have that code. Your code has a syntax error in the first line. Also:
and I got
TypeError: unbound method write() must be called with Telnet instance as first argument (got str instance instead)
That's not the error I get with your code, and that's not the error I get with your code after correcting the obvious typo in the first line.
I really don't understand where is the mistake.
Neither do I. I could guess, but I'd have no idea if I was right or not, because I've no idea what your problem actually is. Give us the exact code that reproduces your problem, so that we know what's going on, and then we'll be able to help you. -Andrew.

Sorry I dont want to send too large posts, so I shorten the problem. I am writing a telnet client, and I got a little problem with keeping connection alive (thread: [Twisted - Python] Telnet client & keeping connection alive). I use a reactor.callLater() loop, and i can't exectue a method i have written in Protocol class. I try to execute this method in the end of code before reactor.run(). Code below: #---------------------# #Client Side Protocol # #---------------------# from twisted.protocols.telnet import Telnet from twisted.internet.protocol import Protocol from twisted.internet.protocol import ClientFactory from twisted.internet import reactor, task, defer, threads import telnetlib as tln from twisted.python import threadable threadable.init() import logging, thread delimiters = ['\r\n', '\r\x00'] class TelnetProtocol(Telnet): # log = logging.getLogger("telnet.protocol.") buffer = '' global ciag licznik = 0 def makeConnection(self, transport): print "Making a connection" Telnet.makeConnection(self, transport) def connectionMade(self): print "Connection made" # self.log.info("telnet_protocol connected") Telnet.connectionMade(self) def connectionLost(self, reason): # self.log.debug("Connection lost: %s", reason) print "connection lost, %s" % reason Telnet.connectionLost(self, reason) def dataReceived(self, data): print "Data recived" ciag="" self.buffer+=data f=open("new.txt","a+") print self.buffer f.write(self.buffer) odp = self.responseFunct(data) self.write(odp) if self.buffer == '': print "nic nie ma" self.transport.loseConnection() if ("login:" in data) and ("Last login:" not in data): print "wchodze 4" # here i take a username from USE CASE self.telnet_User() print "wychodze 4" if "Password:" in data: print "wchodze 5" # here i take a password from USE CASE self.telnet_Password() print "wychodze 5" try: print "try 1" if "[mkl@julia mkl]$" in data: self.telnet_Check_OK(data) print "try 2" else: print "else 1" self.telnet_Check_DENY() print "else 2" pass except: pass def responseFunct(self, buffer): response = "" print "funkcja in" f = open("dlugosc.txt","a+") length = len(buffer)# buffer - string response_buffer="" for i in range(length/3): # every seqence I should RESponse, should begin from IAC - \xff (255) # i dont get a terminal type sequence=buffer[0+(i*3):3+(i*3)] f.write("seq:"+sequence) if sequence.count(tln.IAC): f.write("<-: YES\n") if sequence.count(tln.DO) and sequence.count(tln.SNDLOC): response = tln.IAC + tln.DO + tln.SNDLOC f.write("RES:"+str(response)+"\n") elif sequence.count(tln.DO) and sequence.count(tln.BINARY): response = tln.IAC + tln.WILL + tln.BINARY f.write("RES:"+str(response)+"\n") elif sequence.count(tln.DO) and sequence.count(tln.ECHO): response = tln.IAC + tln.WILL + tln.ECHO f.write("RES:"+str(response)+"\n") elif sequence.count(tln.DO) and sequence.count(tln.SGA): response = tln.IAC + tln.WILL + tln.SGA f.write("RES:"+str(response)+"\n") elif sequence.count(tln.WILL) and sequence.count(tln.BINARY): response = tln.IAC + tln.DO + tln.BINARY f.write("RES:"+str(response)+"\n") elif sequence.count(tln.WILL) and sequence.count(tln.ECHO): response = tln.IAC + tln.DO + tln.ECHO f.write("RES:"+str(response)+"\n") elif sequence.count(tln.WILL) and sequence.count(tln.SGA): response = tln.IAC + tln.DO + tln.SGA f.write("RES:"+str(response)+"\n") elif tln.DO in sequence or tln.DONT in sequence: response = tln.IAC + tln.WONT + sequence[2] f.write("RES:"+str(response)+"\n") elif tln.WILL in sequence or tln.WONT in sequence: response = tln.IAC + tln.DONT + sequence[2] f.write("RES:"+str(response)+"\n") else: f.write("\n") response_buffer +=response print "funkcja out" return response_buffer def telnet_User(self, user="mkl"): print "user1" self.transport.write(user+"\n\r") print "user2" def telnet_Password(self, paswd="Kznjsnm"): print "pass1" self.transport.write(paswd+"\n\r") print "pass2" def loggedIn(self): # i know that user succesfuly login f=open("zewn.txt","w") f.write("logged In") print "logged In" def telnet_Check_OK(self, data): if "[mkl@julia mkl]$" in data: print "wchodze spr log" f=open("new.txt","a+") f.write("udalo sie zalogowac") print "wychodze spr log" pass # self.loggedIn(tf=False) def telnet_Check_DENY(self): print "zly user we" f=open("new.txt","a+") f.write("zly user - nie udalo sie zalogowac") print "zly user wy" self.tansport.loseConnection() reactor.stop() def telnet_KeepAlive(self): print "KA 1" self.write("pwd\n\r") print "KA 2" def telnet_Command(self, command="pwd"): print "comm1" print command self.write(command+"\r\n") f.write(command) print "comm2" #-------------------------# # Client Side Factories 2 # #-------------------------# class MyFactory(ClientFactory): protocol = TelnetProtocol def startedConnecting(self, connector): #self.makeConnection() #connector.connect() pass # we could connector.stopConnecting() def clientConnectionLost(self, connector, reason): print "Connection lost reconecting %s" % reason reactor.stop() # connector.connect() # reconnect def clientConnectionFailed(self, connector, reason): print "connection failed %s" % reason reactor.stop() #-------------------------------------# # Connection API # #-------------------------------------# if __name__=="__main__": def command(commd): # tel=TelnetProtocol() print "command" print commd def telnet_KeepAlive(): print "keeping alive 1" tel=TelnetProtocol() print type(tel) print hasattr(tel,'telnet_KeepAlive') c=tel.telnet_KeepAlive print c #c()#doesn't execute print "keeping alive 2" HOST='127.0.0.1' port=23 l = task.LoopingCall(telnet_KeepAlive) l.start(5.0) # reactor.callLater(5,command(commd="ls")) reactor.connectTCP(HOST, port, MyFactory()) reactor.run()

On Thu, Aug 11, 2005, Michał Tyde wrote:
Sorry I dont want to send too large posts, so I shorten the problem.
This is the right thing to do. But it's not all that you need to do, unfortunately. Your shorter problem did not have the error you claimed it had: in fact, it did not run at all. It is very hard to guess what it is you want when you give a piece of code that doesn't even give the error you are complaining about. You need to provide: 1. a short example of a piece of code that doesn't do what you want 2. a description of what it actually does, ie what the error is 3. a description of what it is you want it to do when it is fixed You gave us 1 and 2, but the problem was that the code didn't actually even do what you said it did at 2. That makes it even harder to guess what it is that you want to happen at 3. -Mary

On Thu, Aug 11, 2005 at 12:26:30PM +0200, Michał Tyde wrote:
Sorry I dont want to send too large posts, so I shorten the problem. I am writing a telnet client, and I got a little problem with keeping connection alive (thread: [Twisted - Python] Telnet client & keeping connection alive). I use a reactor.callLater() loop, and i can't exectue a method i have written in Protocol class. I try to execute this method in the end of code before reactor.run().
Sorry, I'm not sure exactly what the problem you're having is. Shortening the problem is great, but it needs to be something that I can run and see the exact same error you are seeing, rather than SyntaxErrors or completely different errors. With this longer example I'm not sure what part of it is not behaving as you expect. Taking a guess and uncommenting this line:
#c()#doesn't execute
Gives me this error when I run your code: Traceback (most recent call last): File "/tmp/blah.py", line 201, in ? telnet_KeepAlive() File "/tmp/blah.py", line 198, in telnet_KeepAlive c()#doesn't execute File "/tmp/blah.py", line 151, in telnet_KeepAlive self.write("pwd\n\r") File "/usr/lib/python2.4/site-packages/twisted/protocols/telnet.py", line 181, in write self.transport.write(data) AttributeError: 'NoneType' object has no attribute 'write' Is this what you're referring to? I feel like I'm trying to read minds here, and I'm not very good at that :) -Andrew.
participants (3)
-
Andrew Bennetts
-
Mary Gardiner
-
Michał Tyde