0
I am very beginner in Python. I am with a project to create an UDP chat in Python. I want to solve an Issue to create an Online message. I typed the code, only it’s wrong.
#! /usr/bin/env python
#coding:utf-8
from twisted.internet.task import LoopingCall
from twisted.internet import reactor
from twisted.internet.protocol import DatagramProtocol
def mensagem_presenca():
print("Estou Online!!")
class Client(DatagramProtocol):
def sendMessage(self, data, host, port):
self.transport.write(data, host, port)
lc = LoopingCall(sendMessage("teste", localhost, 8888))
lc.start(30.0)
reactor.run()
luiz@luiz:~$ python teste_timer.py Traceback (Most recent call last): File "teste_timer.py", line 16, in Lc = Loopingcall(sendMessage("test", localhost, 8888)) Nameerror: name 'sendMessage' is not defined luiz@luiz:~$
What can it be?
You defined
sendMessage
as a class methodClient
, not as a function, so you need to instantiate the class before.– Woss
Thank you. I’ve established the class. Now it’s giving another error: def message_presenca(): print("I’m Online!!") class Client(Datagramprotocol): def sendMessage(self, data, host, port): self.transport.write(data, host, port) Lc = Loopingcall(Client.sendMessage("test", '127.0.0.1', 8888)) Lc.start(30.0) reactor.run() ----- Traceback (Most recent call): File "teste_timer.py", line 16, in <module> Lc = Loopingcall(Client.sendMessage("test", '127.0.0.1', 8888)) Typeerror: sendMessage() Missing 1 required positional argument: 'port'
– Luiz da Mata
I already put the door. But he’s complaining that I didn’t put.
– Luiz da Mata
Apparently I have to put a value in the place of the self. What I put?
– Luiz da Mata
It seems that you still miss enough the basics of programming. You know what it is to create an instance of a class?
– Woss
Yes. I’m seeing here and I think the problem is that I called a class method and not her instance. That’s it?
– Luiz da Mata
Exact. And when you call the instance method, the
self
is passed implicitly.– Woss