python flask sever client 测试,post 方法,传入python dict或者json字符串
来源:互联网 发布:js隐藏手机号码中间 编辑:程序博客网 时间:2024/05/29 17:24
测试1 传入python dict字典
1 sever服务端
目录结构
|---- /hello.py|----/templates/|----|-----------/index.html
hello.py
from flask import Flask,render_template,requestapp = Flask(__name__)@app.route("/")def index(): return render_template("index.html")@app.route("/login",methods = ['GET', 'POST'])def login(): if request.method == "POST": username = request.form.get('username') password = request.form.get('password') if username=="zhangsan" and password=="123": return "<h1>welcome, %s !</h1>" %username else: return "<h1>login Failure !</h1>" else: return "<h1>login Failure !</h1>"if __name__ == '__main__': app.run(debug=True)
index.html
<form action="/login" method="post"> username: <input type="text" name="username"> password: <input type="password" name="password"> <input type="submit" id="submit"></form>
2 client客户端调用
#coding=utf-8import requestss = requestsdata={"username":"zhangsan","password":"123",}r = s.post('http://127.0.0.1:5000/login', data)print r.status_codeprint r.headers['content-type']print r.encodingprint r.text
先启用服务端程序,然后可以利用客户端程序调用以及返回结果。
测试2 传入json字符串
1 server 端
#!/usr/bin/python# -*- coding: utf-8 -*-import jsonfrom flask import Flaskfrom flask import requestfrom flask import redirectfrom flask import jsonifyapp = Flask(__name__)@app.route('/' , methods=['GET', 'POST'])def index(): if request.method == 'POST': a = request.get_data() dict1 = json.loads(a) return json.dumps(dict1["data"]) else: return '<h1>只接受post请求!</h1>'@app.route('/user/<name>')def user(name): return'<h1>hello, %s</h1>' % nameif __name__ =='__main__': app.run(port='5000')
2 client端
#coding=utf-8import requestsimport jsons = requests#data={"username":"zhangsan","password":"123",}data={ "opr": "add", "data": { "userName": "98997", "disc": "hudihiudhu", "expDate":"2", "ip": [ "10.10.11.1", "10.10.11.2", "10.10.11.3" ] } }data = json.dumps(data)r = s.post('http://127.0.0.1:5000/', data)#r = s.post('http://127.0.0.1:5000/login', data)print r.status_codeprint r.headers['content-type']print r.encodingprint r.text
参考:
http://blog.csdn.net/qq_20711445/article/details/51658498
http://docs.jinkan.org/docs/flask/quickstart.html
http://www.runoob.com/python/python-json.html
阅读全文
0 0
- python flask sever client 测试,post 方法,传入python dict或者json字符串
- python中将json字符串转换为dict
- json dict python base64
- Python flask post接口
- Python Flask之处理客户端通过POST方法传送的数据(json文本,文件)
- Python Web框架 flask post JSON数据获取方式总结
- python构造json格式的post请求访问flask server
- thrift之Hello--python client & cpp sever
- Python dict.has_key()方法
- python dict 方法
- python: dict.get()方法
- Python 初始化 dict 方法
- python 中Dict 转 Json
- python dict与json转换
- python json 转 dict , list
- python dict json读写文件
- python 实现批量post json数据测试
- python flask中HTTP方法methods=['GET','POST']
- 文档对象模型——DOM
- 2017年10月6日提高组T2 猫公司
- node节点
- 用shell命令获得hdfs文件名并动态创建hive表
- 二叉查找树数组表示的JavaScript实现
- python flask sever client 测试,post 方法,传入python dict或者json字符串
- eclipse中的项目鼠标右键卡死(百度:删掉workspace的.metadata文件夹不行,还会卡),sts插件尝试联网引起【先拔网线耐心等待5秒!不行再插上网线】
- I/O复用总结
- 关于GSDMM的数学思考
- java代码读取properties文件
- 明明的随机数
- Java Perfomance
- Invitation Cards POJ
- RS485通信