Reputation: 413
I'm having problems with a ctypes binding, and the ctypes docs are making my head hurt a little.
I have a remote network client sending binary data and the library I'm using (Mosquitto, for MQTT message brokers) provides a ctypes method for getting the original binary data from the network. This is a "LP_c_ubyte" type. Is there any efficient way of turning this back into a python 'str' object?
I need a regular set of bytes to use for M2Crypto's decrypt functions.
pp = ''.join(chr(msg.payload[i]) for i in xrange(msg.payloadlen))
clear_text = rsa.private_decrypt(pp, M2Crypto.RSA.pkcs1_padding)
This works, but it's pretty ugly.
I can go and change the client to base64 encode everything first, and then unbase64 on this end, but that also seems like a bit of a workaround.
Are there any better ways?
Upvotes: 2
Views: 3169
Reputation: 11608
I think this should do what you want:
import ctypes
pp = ctypes.string_at(msg.payload, msg.payloadlen)
Upvotes: 5