Create Function Looping Script

Asked

Viewed 53 times

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 method Client, not as a function, so you need to instantiate the class before.

  • 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'

  • I already put the door. But he’s complaining that I didn’t put.

  • Apparently I have to put a value in the place of the self. What I put?

  • It seems that you still miss enough the basics of programming. You know what it is to create an instance of a class?

  • Yes. I’m seeing here and I think the problem is that I called a class method and not her instance. That’s it?

  • Exact. And when you call the instance method, the self is passed implicitly.

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.