A modern and efficient OSC Client/Server implementation
Project description
OSCPy
=====
/!\ This is very much a work in progress /!\
A modern implementation of OSC for python2/3.
Goals:
- python2.7/3.6+ compatibility (can be relaxed more on the python3 side if needed, but nothing before 2.7 will be supported)
- fast
- easy to use
- robust (returns meaningful errors in case of malformed messages, always do the right thing on correct messages)
- separation of concerns (message parsing vs communication)
- sync and async compatibility (threads, asyncio, trio…)
- clean and easy to read code
features:
- serialize and parse OSC data types/Messages/Bundles
- a thread based udp server to open sockets and bind callbacks on osc addresses on them
- a simple client
usage:
------
Server (thread)
```python
from oscpy.server import OSCThreadServer as OSC
from time import sleep
def callback(values):
print("got values: {}".format(values))
osc = OSCThreadServer()
sock = osc.listen(address='0.0.0.0', port=8000, default=True)
osc.bind(b'/address', callback)
sleep(1000)
osc.stop()
```
or you can use the decorator API.
Server (thread)
```python
from oscpy.server import OSCThreadServer as OSC
from time import sleep
osc = OSCThreadServer()
sock = osc.listen(address='0.0.0.0', port=8000, default=True)
@osc.address(b'/address')
def callback(values):
print("got values: {}".format(values))
sleep(1000)
osc.stop()
```
Servers are also client, in the sense they can send messages and answer to
messages from other servers
```python
from oscpy.server import OSCThreadServer as OSC
from time import sleep
osc_1 = OSCThreadServer()
osc_1.listen(default=True)
@osc_1.address(b'/ping')
def ping(*values):
print("ping called")
if True in values:
cont.append(True)
else:
osc_1.answer(b'/pong')
osc_2 = OSCThreadServer()
osc_2.listen(default=True)
@osc_2.address(b'/pong')
def pong(*values):
print("pong called")
osc_2.answer(b'/ping', [True])
osc_2.send_message(b'/ping', [], *osc_1.getaddress())
timeout = time() + 1
while not cont:
if time() > timeout:
raise OSError('timeout while waiting for success message.')
```
Server (async) (TODO!)
```python
from oscpy.server import OSCThreadServer as OSC
with OSCAsyncServer(port=8000) as OSC:
for address, values in OSC.listen():
if address == b'/example':
print("got {} on /example".format(values))
else:
print("unknown address {}".format(address))
```
Client
```python
from oscpy import OSCClient
osc = OSCClient(address, port)
for i in range(10):
osc.send_message(b'/ping', i)
```
TODO:
- address matching (outside of the currently supported *exact* matching)
- real support for timetag (currently only supports optionally dropping late bundles, not delaying those with timetags in the future)
- support for additional arguments types
- an asyncio-oriented server implementation
- performances assessment (profiling and comparison with other implementations)
- examples & documentation
license
-------
MIT
=====
/!\ This is very much a work in progress /!\
A modern implementation of OSC for python2/3.
Goals:
- python2.7/3.6+ compatibility (can be relaxed more on the python3 side if needed, but nothing before 2.7 will be supported)
- fast
- easy to use
- robust (returns meaningful errors in case of malformed messages, always do the right thing on correct messages)
- separation of concerns (message parsing vs communication)
- sync and async compatibility (threads, asyncio, trio…)
- clean and easy to read code
features:
- serialize and parse OSC data types/Messages/Bundles
- a thread based udp server to open sockets and bind callbacks on osc addresses on them
- a simple client
usage:
------
Server (thread)
```python
from oscpy.server import OSCThreadServer as OSC
from time import sleep
def callback(values):
print("got values: {}".format(values))
osc = OSCThreadServer()
sock = osc.listen(address='0.0.0.0', port=8000, default=True)
osc.bind(b'/address', callback)
sleep(1000)
osc.stop()
```
or you can use the decorator API.
Server (thread)
```python
from oscpy.server import OSCThreadServer as OSC
from time import sleep
osc = OSCThreadServer()
sock = osc.listen(address='0.0.0.0', port=8000, default=True)
@osc.address(b'/address')
def callback(values):
print("got values: {}".format(values))
sleep(1000)
osc.stop()
```
Servers are also client, in the sense they can send messages and answer to
messages from other servers
```python
from oscpy.server import OSCThreadServer as OSC
from time import sleep
osc_1 = OSCThreadServer()
osc_1.listen(default=True)
@osc_1.address(b'/ping')
def ping(*values):
print("ping called")
if True in values:
cont.append(True)
else:
osc_1.answer(b'/pong')
osc_2 = OSCThreadServer()
osc_2.listen(default=True)
@osc_2.address(b'/pong')
def pong(*values):
print("pong called")
osc_2.answer(b'/ping', [True])
osc_2.send_message(b'/ping', [], *osc_1.getaddress())
timeout = time() + 1
while not cont:
if time() > timeout:
raise OSError('timeout while waiting for success message.')
```
Server (async) (TODO!)
```python
from oscpy.server import OSCThreadServer as OSC
with OSCAsyncServer(port=8000) as OSC:
for address, values in OSC.listen():
if address == b'/example':
print("got {} on /example".format(values))
else:
print("unknown address {}".format(address))
```
Client
```python
from oscpy import OSCClient
osc = OSCClient(address, port)
for i in range(10):
osc.send_message(b'/ping', i)
```
TODO:
- address matching (outside of the currently supported *exact* matching)
- real support for timetag (currently only supports optionally dropping late bundles, not delaying those with timetags in the future)
- support for additional arguments types
- an asyncio-oriented server implementation
- performances assessment (profiling and comparison with other implementations)
- examples & documentation
license
-------
MIT
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
oscpy-0.1.0.tar.gz
(161.8 kB
view details)
Built Distribution
File details
Details for the file oscpy-0.1.0.tar.gz
.
File metadata
- Download URL: oscpy-0.1.0.tar.gz
- Upload date:
- Size: 161.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cd742692f44ad8dcf4f755231bef6951cab4035dca4e727cd6f4fe11d883c8ec |
|
MD5 | 54f98fda71b598bac6814a79cd2f00fc |
|
BLAKE2b-256 | cb46e3d6fc0580e5b050cdd079dc44a08cfd91122606678f6a14e673360ffc06 |
File details
Details for the file oscpy-0.1.0-py2.py3-none-any.whl
.
File metadata
- Download URL: oscpy-0.1.0-py2.py3-none-any.whl
- Upload date:
- Size: 8.8 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fc9bc33313968325e3816d82d8f2866ac7792913b19917a8c6d527fbf4ed7d7e |
|
MD5 | 470165682372a74b88fbb9a4c9a2a054 |
|
BLAKE2b-256 | b8eb65ab3b6985d8df9a3ad1b4b9b28d564c900d35fd5f966fbc37b8b15b102e |