gramaziokohler / roslibpy

Python ROS Bridge library
MIT License
273 stars 56 forks source link

add authentication #118

Closed LKSeng closed 10 months ago

LKSeng commented 1 year ago

This is basically a port for the authenticate function in roslibjs. It enables the example in rosauth to be performed on roslibpy, as well as fixes #86.

Following the example in rosauth, one would call authenticate() before calling run():

import roslibpy

auth = {'mac' : '19d9d2166799f1ffd6fee6379f957502aff8716bfebc8cc8b3bac57ade14441bb9678be89d0a7eec9c81291f854d754d7a4de2278bede56f162c2faeb468c68a',
    'client' : 'client',
    'dest' : 'dest',
    'rand' : 'rand',
    't' : 0,
    'level' : 'level',
    'end' : 0
}

client = roslibpy.Ros(host='localhost', port=9090)
client.authenticate(**auth) # need for authentication is specified here. ** is used to unpack the dictionary.
client.run()

# subscriber example.
listener = roslibpy.Topic(client, '/chatter', 'std_msgs/String')
listener.subscribe(lambda message: print('Heard talking: ' + message['data']))

try:
    while True:
        pass
except KeyboardInterrupt:
    client.terminate()

On the rosbridge_server terminal one would see:

2023-08-29 13:55:43+0000 [-] [INFO] [1693317343.325643]: Client connected.  1 clients total.
2023-08-29 13:55:43+0000 [-] [INFO] [1693317343.326674]: Awaiting proper authentication...
2023-08-29 13:55:43+0000 [-] [INFO] [1693317343.329715]: Client 1 has authenticated.

I am unsure what tests and documentation to include for this change.

What type of change is this?

Checklist

Put an x in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your code.

gonzalocasas commented 1 year ago

Awesome! Thanks a lot!