socket 传输图片 Django + socket

-----------------------------------------server----------------------------------------- import numpy as np import socket import demjson import base64host='0.0.0.0' port=9999 ip_port = (host,port) Asocket = socket.socket() Asocket.bind(ip_port) Asocket.listen(5) Emotion = Emotion() print('server listening to '+host+':'+str(port)+'....')while True: try: conn,addr = Asocket.accept() print("connect address:", addr) data = https://www.it610.com/article/conn.recv(1024).decode() img_b64decode = base64.b64decode(data) img_array = np.fromstring(img_b64decode,np.uint8) img = cv2.imdecode(img_array,cv2.COLOR_BGR2RGB) # import cv2 # data = cv2.imread("../Emotion/media/face/" + str(data)) result = Emotion.R(img) result = str(result).encode() conn.sendall(result) conn.close() except Exception as e: print(e)

-----------------------------------------Client----------------------------------------- import cv2 import socket import base64 import numpy as np import matplotlib.pyplot as plts = socket.socket() host = '127.0.0.1' port = 9999 s.connect((host, port))img = open(r'A.jpg','rb') img = base64.b64encode(img.read())s.sendall(img) tips = eval(s.recv(1024).decode()) print(tips)

【socket 传输图片 Django + socket】

    推荐阅读