首页 > 解决方案 > 无法将 base64 字符串发送到 PubNub

问题描述

我正在使用 Raspberry Pi 的 PyCamera 模块来捕获图像并存储为.jpg. 首先使用 base64.encodestring() 对图像进行编码。但是在将编码字符串发送到 PubNub 服务器时,我收到my_publish_callback错误

('ERROR: ', 'Expecting value: line 1 column 1 (char 0)') ('ERROR: ', JSONDecodeError('Expecting value: line 1 column 1 (char 0)',))

我尝试使用 base64.b64encode() 但仍然遇到相同的错误。我已经在 python 2 和 3 中尝试过脚本;

def my_publish_callback(envelope, status):
    if not status.is_error():
       pass  # Message successfully published to specified channel.
    else:
       #print("recv: ", envelope)
       print("ERROR: ", status.error_data.information)
       print("ERROR: ", status.error_data.exception)

def publish(channel, msg):
    pubnub.publish().channel(channel).message(msg).async(my_publish_callback)

def captureAndSendImage():  
  camera.start_preview()
  time.sleep(2)
  camera.capture("/home/pi/Desktop/image.jpg")
  camera.stop_preview()

  with open("/home/pi/Desktop/image.jpg", "rb") as f:
        encoded = base64.encodestring(f.read())
        publish(myChannel, str(encoded))

我无法找到或打印完整的错误回溯,以便我可以获得有关错误发生位置的更多线索。但看起来 PubNub 正在尝试解析 JSON 格式的数据,但它失败了。

标签: jsonpython-3.xraspberry-pi3pubnub

解决方案


我意识到 .jpg 文件大小是 154KB,而 PubNub 最大数据包大小是 32KB,所以应该清楚地说明一切。PubNub 建议通过拆分它们并在订阅者端重新排列它们来发送大消息。感谢@Craig 引用该链接,尽管它很有用 support.pubnub.com/support/discussions/topics/14000006326


推荐阅读