无法将文件流从 NodeJS socketio 服务器发送到 python socketio 客户端
Unable to send file stream from NodeJS socketio server to python socketio client
我一直在尝试使用 socketio 将文件从 nodejs 服务器流式传输到 python socketio 客户端。但是我不知道如何流式传输图像。目前我只是在 Nodejs 中使用 readFile
。这是我到目前为止所写的读取文件并将其作为一个整体发送的内容:
var socket = req.app.get('socket');
readFile(req.file.path, 'utf-8', (err, data) => {
socket.emit('image', { buffer: data, metadata: req.file }, (response) => {
console.log(response);
});
});
在 python socketio 客户端,我想将文件保存到文件夹 assets
并且我编写了以下代码:
@sio.event
async def image(data):
try:
filehandler = open('assets/' + data['metadata']['filename'] + '.jpeg', 'w')
filehandler.write(data['buffer'])
filehandler.close()
except:
return 'NOT OK'
finally:
return 'OK'
但是,最终写入的文件无法作为图像读取,我无法打开它。请根据需要更正我的代码以使其正常工作。
此外,我想从 Nodejs 流式传输文件,而不是读取整个文件然后发送。
如果我能得到这两种方法的代码,我将不胜感激,但至少需要流式传输文件的代码。
谢谢!!
您的问题已在 usage example of socket.io-stream
中得到解答。
编辑:然而在Python这边,似乎有no support:
The Socket.IO protocol doesn't have anything called "streaming". I think you are looking at an extension built by a 3rd party that works on top of the standard Socket.IO packet exchange mechanisms.
我能够为 readFile 解决这个问题。我从 Nodejs 中删除了 'utf-8'
参数,这是更新后的代码:
readFile(req.file.path, (err, data) => {
// I used multer so req.file has the file's metadata but doesn't contain buffer
socket.emit('image', { buffer: data, metadata: req.file }, (response) => {
console.log(response);
});
});
在python socketio客户端,我使用了pillow:
@sio.on('image')
async def tryon(data):
image = Image.open(io.BytesIO(data['buffer']))
image.show()
image.save('assets/' + data['metadata']['filename'] + '.png')
# I tried to write the above code into opencv but was facing errors.
# Skipping it for now. You can refer it if you are willing to further debug it.
# img = cv2.imread(np.frombuffer(data['buffer']))
# cv2.imwrite('assets/' + data['metadata']['filename'] + '.jpeg', img)
return 'OK'
我一直在尝试使用 socketio 将文件从 nodejs 服务器流式传输到 python socketio 客户端。但是我不知道如何流式传输图像。目前我只是在 Nodejs 中使用 readFile
。这是我到目前为止所写的读取文件并将其作为一个整体发送的内容:
var socket = req.app.get('socket');
readFile(req.file.path, 'utf-8', (err, data) => {
socket.emit('image', { buffer: data, metadata: req.file }, (response) => {
console.log(response);
});
});
在 python socketio 客户端,我想将文件保存到文件夹 assets
并且我编写了以下代码:
@sio.event
async def image(data):
try:
filehandler = open('assets/' + data['metadata']['filename'] + '.jpeg', 'w')
filehandler.write(data['buffer'])
filehandler.close()
except:
return 'NOT OK'
finally:
return 'OK'
但是,最终写入的文件无法作为图像读取,我无法打开它。请根据需要更正我的代码以使其正常工作。
此外,我想从 Nodejs 流式传输文件,而不是读取整个文件然后发送。
如果我能得到这两种方法的代码,我将不胜感激,但至少需要流式传输文件的代码。
谢谢!!
您的问题已在 usage example of socket.io-stream
中得到解答。
编辑:然而在Python这边,似乎有no support:
The Socket.IO protocol doesn't have anything called "streaming". I think you are looking at an extension built by a 3rd party that works on top of the standard Socket.IO packet exchange mechanisms.
我能够为 readFile 解决这个问题。我从 Nodejs 中删除了 'utf-8'
参数,这是更新后的代码:
readFile(req.file.path, (err, data) => {
// I used multer so req.file has the file's metadata but doesn't contain buffer
socket.emit('image', { buffer: data, metadata: req.file }, (response) => {
console.log(response);
});
});
在python socketio客户端,我使用了pillow:
@sio.on('image')
async def tryon(data):
image = Image.open(io.BytesIO(data['buffer']))
image.show()
image.save('assets/' + data['metadata']['filename'] + '.png')
# I tried to write the above code into opencv but was facing errors.
# Skipping it for now. You can refer it if you are willing to further debug it.
# img = cv2.imread(np.frombuffer(data['buffer']))
# cv2.imwrite('assets/' + data['metadata']['filename'] + '.jpeg', img)
return 'OK'