Skip to content

Commit

Permalink
docs: pybind readme example (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
JasperTan97 authored Mar 13, 2024
1 parent 1c76cbd commit 36ee3cf
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,33 @@ of the binding sockets. For example, if the containers are running on network *a
- This list of combinations is not exhaustive.
- The binding sockets always have a URI like `*:port` whilst the connecting sockets need to provide the complete address
version (`0.0.0.0:port` if on localhost or `hostname.network:port` if on bridge network).

### Note on the Python bindings

The Python bindings require an additional step of sanitizing the data when sending and receiving bytes. To illustrate this, an example is provided here.

```python
# First a server and a client is connected
context = ZMQContext()
server = ZMQPublisher(ZMQSocketConfiguration(context, "127.0.0.1", "5001", True))
client = ZMQSubscriber(ZMQSocketConfiguration(context, "127.0.0.1", "5001", False))
server.open()
client.open()
```

```python
# Then a string is sent through
str_msg = "Hello!"
server.send_bytes(str_msg)
received_str_msg = client.receive_bytes()
if received_str_msg is not None:
print(received_str_msg)
```
Here we expect the printed value to be `Hello!`, however due to the way strings and bytes are processed, the string message is left as a byte literal and `b'Hello!'` is printed instead. We can correct this as follows:

```python
# Instead, decode the value
print(received_str_msg.decode("utf-8")) # will print Hello! as expected
```

More examples can be found [in the Python unit tests](./python/test).

0 comments on commit 36ee3cf

Please sign in to comment.