Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[paho-dev] paho-mqtt: How to publish an image file with python ?

Hi All,

I cannot find how to publish an image with the python library, this is how I did:

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

import paho.mqtt.client as mqtt

def on_publish(mosq, userdata, mid):
    mosq.disconnect()

client = mqtt.Client()
client.username_pw_set('user', 'password')
client._on_publish_ = on_publish

client.connect("myvps.com", 1883, 60)

f = open('1.jpg', 'rb')
fileContent = f.read()
byteArr = bytes(fileContent)
client.publish("test", byteArr, 0)

client.loop_forever()

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

An error was raised:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 0: ordinal not in range(128)

If I replace the line "byteArr = bytes(fileContent)" with "byteArr = bytearray(fileContent)", no error will be raised but the file won't be published.

My mosquitto service is good and works well with mosquitto_sub and mosquitto_pub commands.

Back to the top