Python Forum

Full Version: python-osc communication
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I'm trying to send some values to a csound instrument using the following code
its from an example found here: https://pypi.org/project/python-osc/

I'm getting an error for:
args = parser.parse_args()

im not sure what this means. But i am aware that i'm confused about the strings in the double quotes. for example where the code says "The ip of the osc server" is it meant to be what it is ? OR do I replace the text with the ip of the osc server if so, how do I get the ip of the osc server?


"""Small example OSC client

This program sends 10 random values between 0.0 and 1.0 to the /filter address,
waiting for 1 seconds between each value.
"""
import argparse
import random
import time

from pythonosc import osc_message_builder
from pythonosc import udp_client


if __name__ == "__main__":
  parser = argparse.ArgumentParser()
  parser.add_argument("--ip", default="127.0.0.1",
      help="The ip of the OSC server")
  parser.add_argument("--port", type=int, default=5005,
      help="The port the OSC server is listening on")
  args = parser.parse_args()

  client = udp_client.SimpleUDPClient(args.ip, args.port)

  for x in range(10):
    client.send_message("/filter", random.random())
    time.sleep(1) 
please, post full traceback you get in error tags as well show us how you run it from command line. the script expects to supply ip of the OSC server as well as port. Default values are 127.0.0.1 for ip and 5005 for port. that is - OSC server is running on your computer (localhost). Is that the case?
Note that what you show is the client code. In the example there is also server part. Does it (or something like it) run somewhere?
I ran the program like this;

$python3 sendosc.py

error tags like so?
Error:
File "sendosc.py", line 20 args = parser.parse_args() SyntaxError: invalid syntax
I have to write it out because i'm running the program on raspbian lite, but im on the forum here with OSX?

The port i want to communcate too in csound is on port 9999 so guess i make that the same for python? should my python script look more like this?

"""Small example OSC client
 
This program sends 10 random values between 0.0 and 1.0 to the /filter address,
waiting for 1 seconds between each value.
"""
import argparse
import random
import time
 
from pythonosc import osc_message_builder
from pythonosc import udp_client
 
 
if __name__ == "__main__":
  parser = argparse.ArgumentParser()
  parser.add_argument("--ip", default="127.0.0.1",
      help="127.0.0.1")
  parser.add_argument("--port", type=int, default=5005,
      help="9999")
  args = parser.parse_args()
 
  client = udp_client.SimpleUDPClient(args.ip, args.port)
 
  for x in range(10):
    client.send_message("/filter", random.random())
    time.sleep(1) 
...
The OSC server is running the same computer (localhost)

what does it mean when you say what i show is the client code, sorry?

the server part is another script that should be running at the same time? how do I do that?

Many many thanks!

...edit. i have ignored this part of the setup? is it necessary ?

$ python setup.py test
$ python setup.py install


a further edit sorry.
I just wanted share a video here for some more context. Im trying to make happen exactly what Pure Date is functioning as for csound here: https://www.youtube.com/watch?v=JX1C3TqP_9Y&t=191s

This video however doesn't mention about client and servers (if I can get an explanation?)

thanks again
I tested your code and it works (I didn't test the osc part, just the argparse). Given that you say you had to write the error down, I guess that the code you post here is not copy/paste from the code you actually run. If you get SynatxError, sometimes the error is on the line before the one in the traceback. If I have to guess - you miss closing parenthesis or something on the line(s) before that. check carefully that you have exactly this:
parser.add_argument("--port", type=int, default=5005,
      help="The port the OSC server is listening on")
if you want, substitute 5005 with 9999 - if that is your port. this way you don't have to supply any command line arguments when running your script.
you say you have the server running on localhost and port 9999 so ignore the server part from my previous post.
(Jul-23-2018, 05:29 PM)fauveboy Wrote: [ -> ]i have ignored this part of the setup? is it necessary ?

$ python setup.py test
$ python setup.py install

if you installed it with pip (obviously you did), this is not necessary.
(Jul-23-2018, 05:29 PM)fauveboy Wrote: [ -> ]I ran the program like this;

$python3 sendosc.py

error tags like so?
Error:
File "sendosc.py", line 20 args = parser.parse_args() SyntaxError: invalid syntax
I have to write it out because i'm running the program on raspbian lite, but im on the forum here with OSX?

Ok, so what's the arrow pointing to? It should let you know exactly what the error is. If not, then the error is probably on the line before (maybe you opened a parentheses but didn't close it?)
Like this, the arrow showing exactly where the issue is:
Error:
>>> 4 + ) File "<stdin>", line 1 4 + ) ^ SyntaxError: invalid syntax >>>
Thank you there was a bracket missing, sorry for the poor debugging, however. It was in line before as what the arrow was pointing at didn't indicating anything out of the ordinary on the line it was on
I've now got the error:

$python3 simpleclient.py
Error:
Traceback (most recent call last): File "simpleclient.py", line 5, in <module> from pythonosc import osc_message_builder ImportError: No module named 'pythonosc'
Unlike the previous error, there's no hidden meaning here. It can't find the module, which means it wasn't installed. What output do you get from running pip install python-osc?
I thought so... the output reads the following:

$pip install python-osc
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Requirement already satisfied: python-osc in ./.local/lib/python2.7/site-packages (1.6.8)
you have both python2 and python3 installed on your raspberry pi.
Currently using pip install python-osc the python-osc package is installed for python2, but you try to run the script with python3
try pip3 install python-osc
that worked Smile the new error however...:

$python3 simpleclient.py 127.0.0.1 9999

Error:
Traceback (most recent call last): File "simpleclient.py", line 16, in <module> client = ump_client.SimpleUDPCClient(args.ip, args.port) AttributeError: 'Namespace' object has no attribute 'ip'
Pages: 1 2