Can't send UDP from Python to Max

Teerath Majumder's icon

Hi all

I am trying to create a bridge between Max and Python for the first time using UDP. I am getting the following error from Python:

OSError: [Errno 48] Address already in use

I found that this happens if some other application is already bound to the specified port. It turns out that the [udpreceive] object in Max is bound to that port (as it should) and not letting Python bind to it. I confirmed it by changing the port number of the [udpreceive] object and then running the Python script; it runs without any issues. Am I doing something wrong? Any ideas on how to get around this?

Python script:

import socket

localIP = "192.168.0.112"
localPort = 8000
bufferSize = 1024

msgFromServer = "Hello from Python!\n"
bytesToSend = str.encode(msgFromServer)

UDPServerSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
UDPServerSocket.bind((localIP, localPort))

UDPServerSocket.close()

System info:

Chip: Apple M1
MacOS: 14.5
Max version: 8.6.3
Python version: 3.12

hz37's icon

I think you're binding it bidirectionally. That and your message is not a valid OSC packet. You'll have more success using the python-osc module:

# pip3 install python-osc

from pythonosc import osc_message_builder

from pythonosc import udp_client

client = udp_client.UDPClient('192.168.0.112', 8000)

msg = osc_message_builder.OscMessageBuilder(address = '/hey')

msg.add_arg('something special')

msg = msg.build()

client.send(msg)