attwad/python-osc

how make an osc response from the server

systemeFriche opened this issue · 3 comments

I want to simulate a Beringher X32 mixing table with a python program. The idea is to command a max program with the X32 Edit software.
To initiate the communication between the X32 Edit software and my fake X32 program, I need the server to respond to the client with an OSC message, something like that :

def xinfo(unused_addr, args):
    server.answer('/xinfo', [X32_OSC_ADDRESS, 'X32-04-A0-71', 'X32', '4.06'])


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--ip",
                        default=X32_OSC_ADDRESS, help="The ip to listen on")
    parser.add_argument("--port",
                        type=int, default=X32_OSC_PORT, help="The port to listen on")
    args = parser.parse_args()

    dispatcher = Dispatcher()
    dispatcher.map("/xinfo", xinfo, "xinfo")

    server = OSCUDPServer((args.ip, args.port), dispatcher)

    print("Serving on {}".format(server.server_address))
    server.serve_forever()

Is it possible with python-osc module ? How ?
Thanks a lot.

I found this solution :

from pythonosc.udp_client import SimpleUDPClient

def xinfo(unused_addr, args):
    client_infos = server.get_request()[1]
    client_ip, client_port = client_infos[0], client_infos[1]
    client_answer = SimpleUDPClient(client_ip, client_port)  # Create client
    client_answer.send_message('/xinfo', [client_ip, 'X32-04-A0-71', 'X32', '4.06'])

Is there another solution less manual ?

In fact, this solution doesn't work because client_answer sends message with a random source port but we need the source port of the response to be the server port. For instance :

client sends an OSC message : (64302, 127.0.0.1) -> to the server (10023, 127.0.0.1)
the server receives this message and answers it with another OSC message : (10023, 127.0.0.1) -> to the client (64302, 127.0.0.1)

With SimpleUDPClient we just specify 127.0.0.1 and 64302. We have to fix is source_port as 10023.

How to make a real response from the server ?

Thanks a lot.

@systemeFriche take a look at the changes merged in #173 which is in the 1.9.0 release. You can send responses to incoming messages on both the client and server side.