How could a 'byte string' input argument be passed to a python function called from MATLAB ?

I am trying to create a server-client application using ipc, where server is a pure Python application and client is a MATLAB application which calls python functions. I have Anaconda installed with Python 3.7 environment.
Python Server code :
from multiprocessing.connection import Listener
address = ('localhost', 6000) # family is deduced to be 'AF_INET'
listener = Listener(address, authkey=b'secret password')
conn = listener.accept()
print('connection accepted from', listener.last_accepted)
while True:
msg = conn.recv()
print(msg)
if msg == 'close':
conn.close()
break
listener.close()
Python client code :
from multiprocessing.connection import Client
address = ('localhost', 6000)
conn = Client(address, authkey=b'secret password')
conn.send('close')
# can also send arbitrary objects:
# conn.send(['a', 2.5, None, int, sum])
conn.close()
When executed in Python, the above pair is working fine.
Now trying MATLAB equivalent of above Python client code in command line :
>> mp_pyModule = py.importlib.import_module('multiprocessing.connection');
>> client_fn = mp_pyModule.Client;
>> address = py.tuple({'localhost',int16(6000)});
>> conn = client_fn(address,pyargs('authkey','secret password'));
Error using connection>Client (line 495)
Python Error: TypeError: authkey should be a byte string
I require the above argument 'secret password' to be sent as a 'byte string'. Is there any method ?
Unsupported types to Python does not mention anything about this. Is there any other limitation ?

4 comentarios

Try passing uint8('secret password')
conn = client_fn(addr,pyargs('authkey',uint8('secret password')));
It is NOT working. Probably it won't ryt, since it is returning a uint8 row vector.
>> uint8('secret password')
ans =
1×15 uint8 row vector
115 101 99 114 101 116 32 112 97 115 115 119 111 114 100
Thanks..
Try py.bytes(uint8('secret password'))
Kudos Walter !! It worked.

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Productos

Versión

R2017a

Preguntada:

el 29 de Mzo. de 2019

Comentada:

el 1 de Abr. de 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by