python flask request 传输字符串

python flask request 传输字符串

pytorch server官网:https://pytorch.org/tutorials/intermediate/flask_rest_api_tutorial.html

图片转为字符串,存入json文件,再读取:
https://blog.csdn.net/weixin_38383877/article/details/82262382

Python Flask之处理客户端通过POST方法传送的数据(json文本,文件):
https://blog.csdn.net/tanyjin/article/details/78640987

python使用post请求发送图片并接受图片 - 云中寻雾的博客 https://blog.csdn.net/qq_36387683/article/details/95066303

【Pytorch】使用DeepLabv3-ResNet101语义分割进行背景替换 - heiheiya的博客 https://blog.csdn.net/heiheiya/article/details/102973666

1、新建 test.py

from flask import Flask, request

app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'hello world'

@app.route('/register', methods=['POST'])
def register():
    if request.method == 'POST':
        print ("request.headers: ", request.headers)
        print ("request.form: ", request.form)
        print ("request.form['password']: ", request.form['password'], type(request.form['password']))
        print ("request.form.get('name'): ", request.form.get('name'))
        print ("request.form.getlist('name'): ", request.form.getlist('name'))
        print ("request.form.get('nickname', default='little apple'): ",request.form.get('nickname', default='little apple'))
        return 'welcome'

if __name__ == '__main__':
    app.run(debug=True)

2、新建 request.py

import requests

user_info = {'name': 'letian', 'password': '123'}
r = requests.post("http://127.0.0.1:5000/register", data=user_info)

print (r.text)

个人笔记:

################一、输入读取字符串###################
# client:
img_path = {'path':'/media/cv/DataB/xj/pytorch_server/service-streamer-master/example_vision/images'}
r = requests.post("http://172.23.132.130:5005/stream_predict", data=img_path)
print (r.text)

# server:  
@app.route('/stream_predict', methods=['POST'])
def stream_predict():
    if request.method == 'POST':
        imgpath = request.form['path']  #得到字符串
        img_bytes = img_process(imgpath)
        batch_result = streamer.predict(img_bytes)
        #print(batch_result)
        return 'done'



################二、输入读取图片###################
# client:
resp = requests.post("http://localhost:5005/stream_predict",
                     files={"file": open("cat.jpg",'rb')})
print(resp.json())

# server:
@app.route('/stream_predict', methods=['POST'])
def stream_predict():
    if request.method == 'POST':
        file = request.files['file']
        img_bytes = file.read()
        batch_result = streamer.predict(img_bytes)
        print(batch_result)
        return 'done'
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值