json中load和loads区别
如果返回Json先出status code500,return的时候用jsonify(data)
在客户端如果想要读返回的json的话,
respon = requests.request("POST", server_address, json=data)
res_json = json.loads(respon.text)
Python Flask Web 框架入门:https://blog.csdn.net/sinat_38682860/article/details/82354342
下面代码功能为:
客户端发送一张图片到服务端,服务端返回一个url及图片
先运行server.py.py
,再运行client.py
server.py.py
内容
from flask import request, Flask, jsonify
import base64
import os
from download import download_image
app = Flask(__name__)
def decode_img(img_path):
# 输入:本地图片保存地址
# 输出:图片二进制base64base64格式
with open(img_path, mode='rb') as file:
img = file.read()
return base64.encodebytes(img).decode("utf-8")
def do_something(image_path, server_send_client_img='./static/server_send_client_img'):
# 下面为测试本地服务的代码
url = 'http://wx4.sinaimg.cn/large/006CXrEjly1gdj8q4gbrtj30hs0hsabc.jpg'
data = {}
img_path = os.path.join(server_send_client_img, os.path.basename(url))
download_image(url, img_path)
img_base64 = decode_img(img_path)
data['url'], data['img_base64'] = url, img_base64
return data
@app.route("/", methods=['POST'])
def do_something_server(server_get_client_img='./static/server_get_client_img'):
os.makedirs(server_get_client_img, exist_ok=True)
post_data = request.json
if post_data:
image_data = base64.b64decode(post_data["image_base64"])
image_path = post_data['image_path']
image_path = os.path.join(server_get_client_img, os.path.basename(image_path))
with open(image_path, 'wb') as file:
file.write(image_data)
print('【服务端】接收到图片,已保存至{}'.format(image_path))
data = do_something(image_path)
print('【服务端】发送返回图到客户端')
return jsonify(data)
else:
return 'fail'
if __name__ == "__main__":
app.run("127.0.0.1", port=5000)
client.py
内容
import json
import base64
import requests
import os
def decode_img(img_path):
# 输入:本地图片保存地址
# 输出:图片二进制base64base64格式
with open(img_path, mode='rb') as file:
img = file.read()
return base64.encodebytes(img).decode("utf-8")
def write_img(res_json, client_get_img='./static/client_get_img'):
# 输入:图片二进制base64base64格式
# 输出:图片保存到本地
image_data = base64.b64decode(res_json['img_base64'])
image_path = os.path.join(client_get_img, os.path.basename(res_json['url']))
with open(image_path, 'wb') as file:
file.write(image_data)
print('【客户端】已保存返回的图片至: {}'.format(image_path))
def deal_with_respoon(respon):
# 输入:服务端的返回
# 输出:服务端传来的Url、图片二进制base64base64格式(图片保存到本地)
res_json = json.loads(respon.text)
print('【客户端】已发送图片 {} 至 {}'.format(img_path, server_address))
print('【客户端】已接收到返回图片的url: {}'.format(res_json['url']))
write_img(res_json)
def client_request(server_address, img_path):
# 输入:服务端地址,本地上传的图片地址
# 输出:服务端传来的Url、图片二进制base64base64格式(图片保存到本地)
data = {}
data['image_base64'] = decode_img(img_path)
data['image_path'] = img_path
respon = requests.request("POST", server_address, json=data)
deal_with_respoon(respon)
if __name__ == '__main__':
img_path = r'.\static\client_send_img\1.jpg'
server_address = "http://127.0.0.1:5000"
client_request(server_address, img_path)
download.py
import urllib.request
import os
def download_image(url, download_path, again=0):
img_name = download_path
if os.path.exists(img_name):
return img_name, True
try:
urllib.request.urlretrieve(url, filename=img_name)
return img_name, True
except:
if again == 5:
print('Error Download:', url)
return img_name, False
else:
print('Download again:', url)
return img_name, download_image(url, download_path=download_path ,again=again + 1)
文件目录如下:
上面三个py文件是自己写的代码,其他的都是新建flask时自带的
static下的四个文件夹也是自己新建的,
client_send_img
文件下下需要提前放好需要发送的图片
server_send_client_img
中的图片由server.py.py
中的url下载的
server_get_client_img
和client_get_img
在成功通信后保存对方发送的图片
结果为