Created
December 27, 2019 18:13
-
-
Save agronholm/c16940e053fd2c33b11dfc24401b14e4 to your computer and use it in GitHub Desktop.
Simple UDP receive/send pair for IPv6 with the receive side using asyncio
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import socket | |
class CustomProto(asyncio.DatagramProtocol): | |
def connection_made(self, transport): | |
print('ready to receive datagrams on', transport.get_extra_info('sockname')) | |
def error_received(self, exc): | |
print('Error:', exc) | |
def datagram_received(self, data, addr): | |
print('Datagram received:', data) | |
loop = asyncio.get_event_loop() | |
loop.set_debug(True) | |
transport, protocol = loop.run_until_complete(loop.create_datagram_endpoint(CustomProto, local_addr=('::1', 9999))) | |
loop.run_forever() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
remote_addr = ('::1', 9999) | |
with socket.socket(socket.AF_INET6, socket.SOCK_DGRAM) as udp: | |
udp.sendto(b'test message', remote_addr) | |
print('Sent a message to', remote_addr) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment