UDP – UDP handle

class uv.UDP(flags=0, loop=None, on_receive=None)[source]

Abstraction of UDP sockets for servers and clients.

on_receive

Callback called after package has been received.

on_receive(udp_handle, status, addr, data, flags)
Parameters:
  • udp_handle (uv.UDP) – handle the call originates from
  • status (uv.StatusCode) – status of the handle (indicate any errors)
  • addr (uv.Address4 | uv.Address6 | tuple) – address the data originates from
  • data – data which has been received
  • flags (int) – udp status flags (e.g. partial read)
Readonly:False
Type:Callable[[uv.UDP, uv.StatusCode, uv.Address, bytes, int], None]
open(fd)[source]

Open an existing file descriptor as an udp handle.

Raises:
  • uv.UVError – error while opening the handle
  • uv.ClosedHandleError – handle has already been closed or is closing
Parameters:

fd (int) – file descriptor

bind(address, flags=0)[source]

Bind the socket to the specified address.

Raises:
  • uv.UVError – error while binding to address
  • uv.ClosedHandleError – handle has already been closed or is closing
Parameters:

address – address to bind to (ip, port, flowinfo=0, scope_id=0)

:param flags
bind flags to be used (mask of uv.UDPFlags)
send(buffers, address, on_send=None)[source]

Send data over the UDP socket. If the socket has not previously been bound with bind() it will be bound to 0.0.0.0 (the “all interfaces” IPv4 address) and a random port number.

Raises:
  • uv.UVError – error while initializing the request
  • uv.ClosedHandleError – udp handle has already been closed or is closing
Parameters:
  • buffers (tuple[bytes] | list[bytes] | bytes) – data which should be send
  • address (tuple | uv.Address4 | uv.Address6) – address tuple (ip, port, flowinfo=0, scope_id=0)
  • on_send (Callable[[uv.UDPSendRequest, uv.StatusCode], None]:rtype: uv.UDPSendRequest) – callback called after all data has been sent
try_send(buffers, address)[source]

Same as send(), but won’t queue a write request if it cannot be completed immediately.

Raises:
  • uv.UVError – error while sending data
  • uv.ClosedHandleError – handle has already been closed or is closing
Parameters:
  • buffers (tuple[bytes] | list[bytes] | bytes) – data which should be send
  • address (tuple | uv.Address4 | uv.Address6) – address tuple (ip, port, flowinfo=0, scope_id=0)
Returns:

number of bytes sent

Return type:

int

receive_start(on_receive=None)[source]

Prepare for receiving data. If the socket has not previously been bound with bind() it is bound to 0.0.0.0 (the “all interfaces” IPv4 address) and a random port number.

Raises:
  • uv.UVError – error while start receiving datagrams
  • uv.ClosedHandleError – handle has already been closed or is closing
Parameters:

on_receive (Callable[[uv.UDP, uv.StatusCode, uv.Address, bytes, int], None]) – callback called after package has been received

receive_stop()[source]

Stop listening for incoming datagrams.

Raises:uv.UVError – error while stop listening for incoming datagrams
set_membership(multicast_address, membership, interface_address=None)[source]

Set membership for a multicast address

raises uv.UVError:
error while setting membership
Raises:

uv.ClosedHandleError – handle has already been closed or is closing

Parameters:
  • multicast_address (unicode) – multicast address to set membership for
  • membership (uv.UDPMembership) – membership operation
  • interface_address (unicode) – interface address
set_multicast_loop(enable)[source]

Set IP multicast loop flag. Makes multicast packets loop bac to local sockets.

Raises:
  • uv.UVError – error enabling / disabling multicast loop
  • uv.ClosedHandleError – handle has already been closed or is closing
Parameters:

enable (bool) – enable / disable multicast loop

set_multicast_ttl(ttl)[source]

Set the multicast ttl.

Raises:uv.UVError – error while setting ttl
:raises uv.ClosedHandleError
handle has already been closed or is closing
Parameters:ttl (int) – multicast ttl (between 1 and 255)
set_multicast_interface(interface)[source]

Set the multicast interface to send or receive data on.

Raises:
  • uv.UVError – error while setting multicast interface
  • uv.ClosedHandleError – handle has already been closed or is closing
Parameters:

interface (unicode) – multicast interface address

set_broadcast(enable)[source]

Set broadcast on or off.

Raises:
  • uv.UVError – error enabling / disabling broadcast
  • uv.ClosedHandleError – handle has already been closed or is closing
Parameters:

enable (bool) – enable / disable broadcast

family

Address family of UDP handle, may be None.

Readonly:True
Return type:int | None
sockname

The local IP and port of the UDP handle.

Raises:uv.UVError – error while receiving sockname
Readonly:True
Return type:uv.Address4 | uv.Address6
class uv.UDPFlags[source]

UDP configuration and status flags enumeration.

IPV6ONLY = None

Disable dual stack support.

Type:uv.UDPFlags
REUSEADDR = None

Enable SO_REUSEADDR when binding the handle. This sets the SO_REUSEPORT socket flag on the BSDs and OSX. On other Unix platforms, it sets the SO_REUSEADDR flag. This allows multiple threads or processes to bind to the same address without errors (provided that they all set the flag) but only the last one will receive any traffic, in effect “stealing” the port from the previous listener.

Type:uv.UDPFlags
PARTIAL = None

Indicates that the received message has been truncated because the read buffer was too small. The remainder was discarded by the OS.

Type:uv.UDPFlags
class uv.UDPMembership[source]

Membership types enumeration for multicast addresses.

LEAVE_GROUP = None

Leave multicast group.

Type:uv.UDPMembership
JOIN_GROUP = None

Join multicast group.

Type:uv.UDPMembership