Does asyncio from python support coroutine-based API for UDP networking?

前端 未结 2 1152
一向
一向 2021-02-13 22:46

I was browsing the python asyncio module documentation this night looking for some ideas for one of my course projects, but I soon find that there might be a lack o

2条回答
  •  独厮守ぢ
    2021-02-13 23:03

    You might be interested in this module providing high-level UDP endpoints for asyncio:

    async def main():
        # Create a local UDP enpoint
        local = await open_local_endpoint('localhost', 8888)
    
        # Create a remote UDP enpoint, pointing to the first one
        remote = await open_remote_endpoint(*local.address)
    
        # The remote endpoint sends a datagram
        remote.send(b'Hey Hey, My My')
    
        # The local endpoint receives the datagram, along with the address
        data, address = await local.receive()
    
        # Print: Got 'Hey Hey, My My' from 127.0.0.1 port 50603
        print(f"Got {data!r} from {address[0]} port {address[1]}")
    

提交回复
热议问题