Twisted Python: UDP Broadcast (Simple Echo Server)

I am trying to adapt Python Twisted - UDP examples use UDP broadcast. I can send a message from the client and receive it on the server, however it does not send the message.

Customer:

from twisted.internet.protocol import DatagramProtocol
from twisted.internet import reactor

from socket import SOL_SOCKET, SO_BROADCAST

class EchoClientDatagramProtocol(DatagramProtocol):
    strings = [
        "Hello, world!",
        "What a fine day it is.",
        "Bye-bye!"
    ]

    def startProtocol(self):
        self.transport.socket.setsockopt(SOL_SOCKET, SO_BROADCAST, True)
        self.transport.connect("255.255.255.255", 8000)
        self.sendDatagram()

    def sendDatagram(self):
        if len(self.strings):
            datagram = self.strings.pop(0)
            self.transport.write(datagram)
        else:
            reactor.stop()

    def datagramReceived(self, datagram, host):
        print 'Datagram received: ', repr(datagram)
        self.sendDatagram()

def main():
    protocol = EchoClientDatagramProtocol()
    #0 means any port
    t = reactor.listenUDP(0, protocol)
    reactor.run()

if __name__ == '__main__':
   main()

Server:

from twisted.internet.protocol import DatagramProtocol
from twisted.internet import reactor

class EchoUDP(DatagramProtocol):
    def datagramReceived(self, datagram, address):
        print "Received from address: " + str(address)
        print str(datagram)
        self.transport.write(datagram, address)
        print "Finished sending reply."

print "Starting server."
reactor.listenUDP(8000, EchoUDP())
reactor.run()

Console output:

Server:

Starting server.
Received from address ('192.168.1.137', 53737)
Hello, world!
Finished sending reply.

Client:

no output.
+4
source share
1 answer

transport.connectcreates a connected UDP socket

UDP- - / , . , , , . UDP , . , , , .

, , , .

self.transport.write(data, (host, port)) - .

from twisted.internet.protocol import DatagramProtocol
from twisted.internet import reactor

from socket import SOL_SOCKET, SO_BROADCAST

class EchoClientDatagramProtocol(DatagramProtocol):
    strings = [
        "Hello, world!",
        "What a fine day it is.",
        "Bye-bye!"
    ]

    def startProtocol(self):
        self.transport.socket.setsockopt(SOL_SOCKET, SO_BROADCAST, True)
        #self.transport.connect("255.255.255.255", 8000) <- not needed
        self.sendDatagram()

    def sendDatagram(self):
        if len(self.strings):
            datagram = self.strings.pop(0)
            self.transport.write(datagram, ('255.255.255.255', 8000)) # <- write to broadcast address here
        else:
            reactor.stop()

    def datagramReceived(self, datagram, host):
        print 'Datagram received: ', repr(datagram)
        self.sendDatagram()

def main():
    protocol = EchoClientDatagramProtocol()
    #0 means any port
    t = reactor.listenUDP(0, protocol)
    reactor.run()


if __name__ == '__main__':
   main()
+2

Source: https://habr.com/ru/post/1526860/


All Articles