是否可以通过套接字发送包含文件(图像或文档)作为值的dict?
我试过像bellow这样的东西,但是失败了..
with open("cat.jpeg", "rb") as f:
myFile = f.read(2048)
data = {"id": "1283", "filename": "cat.jpeg", "file": myFile}
dataToSend = json.dumps(data).encode("utf-8")
这会产生一个json错误,myFile是一个字节数组,不能被序列化。
我尝试使用base64编码将myFile转换为字符串,但不起作用。
部分起作用的是将myFile转换为字符串,如str(myFile)。json序列化程序起作用了,我通过套接字发送了它,字典是正常的,但是myFile数据被损坏了,所以我无法重新创建图片。
那么,是否可以使用这种方法,或者我应该如何通过套接字发送文件和数据,以便在另一端轻松解析?
LE:
仍然不能使用base64编码,myFile仍然是“字节”格式,json给出这个错误: TypeError:'bytes‘类型的对象不是JSON可序列化的
客户端
import os
import base64
import json
import socket
currentPath = os.path.dirname(os.path.abspath(__file__)) + "\\downloads\\"
with open(currentPath + "cat.png", "rb") as f:
l = f.read()
print(type(l)) #prints <class 'bytes'>
myFile = base64.b64encode(l)
print(type(myFile)) #prints <class 'bytes'>
data = {"id": "12", "filename": "cat.png", "message": "So cute!", "file": myFile}
dataToSend = json.dumps(data).encode("utf-8") #prints TypeError: Object of type 'bytes' is not JSON serializable
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("127.0.0.1", 1234))
s.sendall(dataToSend)
s.close()
和服务器:
import socket
import json
import os
import sys
import time
import base64
currentPath = os.path.dirname(os.path.abspath(__file__)) + "\\fileCache\\"
tempData = bytearray()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("127.0.0.1", 1234))
s.listen(5)
conn, addr = s.accept()
while True:
dataReceived = conn.recv(2048)
if sys.getsizeof(dataReceived) > 17:
tempData = tempData + dataReceived
else:
data = json.loads(tempData.decode("utf-8"))
break
time.sleep(1)
print(data)
myFile = base64.b64decode(data["file"])
with open(currentPath + data["filename"], "wb") as f:
f.write(myFile)
f.close()
发布于 2018-06-03 02:06:32
您应该能够执行以下操作:
data = base64.b64encode(myFile)
dataToSend = json.dumps({"id":"1283","filename":"cat.jpeg", "file":data})
然后通过套接字发送。当您在套接字的另一端接收数据时,只需执行以下操作:
jsonDict = json.loads(dataReceived)
data = base64.b64decode(jsonDict["file"])
更好的方法可能是只使用bson,https://github.com/py-bson/bson。
from gevent import monkey, socket
monkey.patch_all()
import bson
bson.patch_socket()
with open("cat.jpeg", "rb") as f:
myFile = f.read()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("127.0.0.1", 12345))
s.sendobj({u"id": "1283", u"filename": "cat.jpeg", u"file": myFile})
发布于 2018-06-03 19:28:05
感谢大家的帮助,我终于用base64完成了。我在堆栈溢出上找到了答案,我忘记了它的链接,但它开始了。
在使用json.dumps之前,我必须像这样对文件进行编码和解码。
base64_bytes = b64encode(l)
myFile = base64_bytes.decode("utf-8")
下面是一个有效的示例:
客户端:
import os
from base64 import b64encode
import json
import socket
currentPath = os.path.dirname(os.path.abspath(__file__)) + "\\downloads\\"
with open(currentPath + "cat.png", "rb") as f:
l = f.read()
base64_bytes = b64encode(l)
myFile = base64_bytes.decode("utf-8")
data = {"id": "12", "filename": "cat.png", "message": "So cute!", "file": myFile}
dataToSend = json.dumps(data).encode("utf-8")
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("127.0.0.1", 1234))
s.sendall(dataToSend)
s.close()
服务器:
import socket
import json
import os
import sys
import base64
currentPath = os.path.dirname(os.path.abspath(__file__)) + "\\fileCache\\"
tempData = bytearray()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("127.0.0.1", 1234))
s.listen(5)
conn, addr = s.accept()
while True:
dataReceived = conn.recv(4096)
if sys.getsizeof(dataReceived) > 17:
tempData = tempData + dataReceived
else:
data = json.loads(tempData.decode("utf-8"))
break
myFile = base64.b64decode(data["file"])
with open(currentPath + data["filename"], "wb") as f:
f.write(myFile)
f.close()
https://stackoverflow.com/questions/50659391
复制相似问题