Flask-SocketIO send images

让人想犯罪 __ 提交于 2020-12-10 07:43:58

问题


I am currently working on a project that uses Flask-SocketIO to send things over the internet, but I came across this question.

Question:

Is there any way to send images in Flask-SocketIO? I did some googling but no luck for me.


回答1:


Socket.IO is a data agnostic protocol, so you can send any kind of information. Both text and binary payloads are supported.

If you want to send an image from the server, you can do something like this:

with open('my_image_file.jpg', 'rb') as f:
    image_data = f.read()
emit('my-image-event', {'image_data': image_data})

The client will have to be aware that you are sending jpeg data, there is nothing in the Socket.IO protocol that makes sending images different than sending text or other data formats.

If you are using a JavaScript client, you will get the data as a byte array. Other clients may choose the most appropriate binary representation for this data.



来源:https://stackoverflow.com/questions/53399948/flask-socketio-send-images

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!