| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
# Copyright (c) Twisted Matrix Laboratories. |
| 4 |
# See LICENSE for details. |
| 5 |
|
| 6 |
from twisted.internet import reactor |
| 7 |
from twisted.internet.protocol import DatagramProtocol |
| 8 |
|
| 9 |
|
| 10 |
class EchoClientDatagramProtocol(DatagramProtocol): |
| 11 |
strings = [b"Hello, world!", b"What a fine day it is.", b"Bye-bye!"] |
| 12 |
|
| 13 |
def startProtocol(self): |
| 14 |
self.transport.connect("127.0.0.1", 8000) |
| 15 |
self.sendDatagram() |
| 16 |
|
| 17 |
def sendDatagram(self): |
| 18 |
if len(self.strings): |
| 19 |
datagram = self.strings.pop(0) |
| 20 |
self.transport.write(datagram) |
| 21 |
else: |
| 22 |
reactor.stop() |
| 23 |
|
| 24 |
def datagramReceived(self, datagram, host): |
| 25 |
print("Datagram received: ", repr(datagram)) |
| 26 |
self.sendDatagram() |
| 27 |
|
| 28 |
|
| 29 |
def main(): |
| 30 |
protocol = EchoClientDatagramProtocol() |
| 31 |
reactor.listenUDP(0, protocol) |
| 32 |
reactor.run() |
| 33 |
|
| 34 |
|
| 35 |
if __name__ == "__main__": |
| 36 |
main() |