json - 无法将 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 格式的数据,但它失败了。
解决方案
我意识到 .jpg 文件大小是 154KB,而 PubNub 最大数据包大小是 32KB,所以应该清楚地说明一切。PubNub 建议通过拆分它们并在订阅者端重新排列它们来发送大消息。感谢@Craig 引用该链接,尽管它很有用 support.pubnub.com/support/discussions/topics/14000006326
推荐阅读
- php - 如何从 foreach 循环内的两个数组中准备一个新数组?
- angular - 如何在Angular 2+中将服务注入自定义.ts文件
- java - HashMap values().equals() 总是返回 false,即使与另一个具有相同值的 HashMap 比较
- python-3.x - 如何找到k-means的k值?
- .net - Powershell:以执行时间限制启动进程并在运行时从其 StdOut 读取
- django - 从 Redis 后端删除所有 Celery 结果
- python - 在 matplotlib 中将子图插入图时控制比例
- python - 在主题提取的评论中删除外来词的最佳方法是什么?
- settings - 我无法恢复我的 Android Studio 预览设置
- angular - 在移动制表符时调用方法 - 角度