API Reference

This is the API reference generated from the source code.

A better API for asynchronous UDP.

class aioudp.Connection(send_func: Callable[[bytes], None], recv_func: Callable[[], Awaitable[bytes | None]], is_closing: Callable[[], bool], get_local_addr: Callable[[], Tuple[str, int]], get_remote_addr: Callable[[], Tuple[str, int]], closed: bool = False)

Represents a server-client connection. Do not instantiate manually.

property local_address: Tuple[str, int]

Returns the local address of the connection. This is your IP.

See also

remote_address()

Returns:

This is a tuple containing the hostname and port

Return type:

tuple[str, int]

async recv() bytes

Receives a message from the connection.

Returns:

The received bytes.

Return type:

bytes

Raises:

exceptions.ConnectionClosedError – The connection is closed

property remote_address: Tuple[str, int]

Returns the remote address of the connection. This is their IP.

See also

local_address()

Returns:

This is a tuple containing the hostname and port

Return type:

tuple[str, int]

async send(data: bytes) None

Send a message to the connection.

The reason why this send function is async is due to API consistency. There is actually no underlying async call so feel free to just forgo the await.

Warning

Since this is UDP, there is no guarantee that the message will be sent

Parameters:

data (bytes) – The message in bytes to send

Raises:
aioudp.connect(host: str, port: int, queue_size: int | None = None, **kwargs: Any) AsyncIterator[Connection]

Connect to a UDP server.

See also

serve()

Parameters:
  • host (str) – The server’s host name/address.

  • port (int) – The server’s port number.

  • queue_size (int | None) – The maximum size of the message queue used internally. Defaults to None, meaning an unlimited size. Unless you know for sure what you’re doing, there is no need to change this value.

  • **kwargs

    Additional keyword arguments to pass to

    loop.create_datagram_endpoint.

Returns:

An asynchronous iterator yielding a connection to the UDP server.

aioudp.serve(host: str, port: int, handler: Callable[[Connection], Coroutine[Any, Any, None]], queue_size: int | None = None, **kwargs: Any) AsyncIterator[None]

Run a UDP server.

See the docs for an example UDP echo server

Parameters:
  • host (str) – The host name/address to run the server on

  • port (int) – The port number to run the server on

  • handler (Callable[[connection.Connection], Awaitable[None]]) – An asynchronous function to handle a request It should accept an instance of connection.Connection and doesn’t need to return anything.

  • queue_size (int | None) – The maximum size of the message queue used internally. Defaults to None, meaning an unlimited size. Unless you know for sure what you’re doing, there is no need to change this value.

  • **kwargs

    Additional keyword arguments to pass to

    loop.create_datagram_endpoint.

Exceptions

Exceptions this library might raise.

exception aioudp.exceptions.AioUDPError

Base exception for this library.

All exceptions this library will raise will subclass this

exception aioudp.exceptions.ConnectionClosedError

When a connection is closed and you tried to send/recieve a message.