Skip to content

Commit b172fa7

Browse files
author
John Allsup
committed
Added force_ipv4 and force_ipv6 options to UdpClient
1 parent 4b40a71 commit b172fa7

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

pythonosc/udp_client.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515

1616
from typing import Union
1717

18+
class UdpClientException(Exception):
19+
pass
1820

1921
class UDPClient(object):
2022
"""OSC client to send :class:`OscMessage` or :class:`OscBundle` via UDP"""
2123

22-
def __init__(self, address: str, port: int, allow_broadcast: bool = False) -> None:
24+
def __init__(self, address: str, port: int, allow_broadcast: bool = False, force_ipv4 = False, force_ipv6 = False) -> None:
2325
"""Initialize client
2426
2527
As this is UDP it will not actually make any attempt to connect to the
@@ -29,8 +31,20 @@ def __init__(self, address: str, port: int, allow_broadcast: bool = False) -> No
2931
address: IP address of server
3032
port: Port of server
3133
allow_broadcast: Allow for broadcast transmissions
34+
force_ipv4: require that remote address is IPv4
35+
force_ipv6: require thta remote address is IPv6
3236
"""
33-
for addr in socket.getaddrinfo(address, port, type=socket.SOCK_DGRAM):
37+
38+
if force_ipv4 and force_ipv6:
39+
raise ValueError("Can only force one of IPv4 or IPv6")
40+
elif force_ipv4:
41+
family = socket.AF_INET
42+
elif force_ipv6:
43+
family = socket.AF_INET6
44+
else:
45+
family = 0
46+
47+
for addr in socket.getaddrinfo(address, port, type=socket.SOCK_DGRAM, family=family):
3448
af, socktype, protocol, canonname, sa = addr
3549

3650
try:

0 commit comments

Comments
 (0)