Search code examples
pythonnetwork-programmingudposc

How do I make my python OSC Server listen to my own IP address?


im new to networking.

i have a simple python script using pyOSC receiving OSC messages, but using localhost or 127.0.0.1 do not work when i want to listen inside my router's network. When I use my local Network IP 192.168.178.xx it works like a charm.

I presume localhost/127.0.0.1 does only work inside of my own machine/network-device (?).

But how can I change my receive address dynamically to my recent IP adress?

here the code (with the not working localhost:

import OSC, threading

receive_address = ('localhost', 12035) 
send_address = '192.168.178.20', 12036 

# Initialize the OSC server and the client.
s = OSC.OSCServer(receive_address)
c = OSC.OSCClient()
c.connect(send_address)

# simple send function for multiple arguments
def send_osc(addr, *stuff):
    msg = OSC.OSCMessage()
    msg.setAddress(addr)
    for item in stuff:
        msg.append(item)
    c.send(msg)

# simple callback functions
def answer_handler(addr, tags, stuff, source):

    print('inside incoming_handler')
    print "---"
    print "received new osc msg from %s" % OSC.getUrlStr(source)

# Start OSCServer in extra thread
st = threading.Thread( target = s.serve_forever )
st.start()
# adding callback functions to listener
s.addMsgHandler("/GAMEMASTER", answer_handler)

Solution

  • i found some solutions here: Python - Get localhost IP

    in particular the answer by user Alexander

    if there is a more elegant way, I am very happy to see it. the above solutions work for my code, but seem a bit hacked.