#post_data.py
# -*- coding: utf8 -*-
import requests
content = "helloworld"
postdic = {
"ToUserName":"qianxun",
"FromUserName":"xiaowang",
"MsgType":"text",
"Content":content
}
r = requests.post("http://127.0.0.1:5000",params=postdic)
r = requests.get("http://127.0.0.1:5000",params=postdic)
print r.text #此处返回flask框对应return的数据
****************************************************************************************
****************************************************************************************
#向后台发送xml格式的数据
postdic = u"""
<xml>
<ToUserName>wang</ToUserName>
<FromUserName>xiao</FromUserName>
<MsgType>text</MsgType>
<Content>hello</Content>
</xml>
"""
r = requests.post("http://127.0.0.1:5000",postdic)
flask框架操作
def xml_convert_dic(request.data):#前台申请的request数据,在此处运算操作
request_data = request.data
root = ET.fromstring(request_data)#注意此处是request.data,此处时把xml格式的数据转化到字典中以便之后的操作
msg = {}
for child in root:
msg[child.tag] = child.text
print msg
return msg
flask框架:https://dormousehole.readthedocs.org/en/latest/quickstart.html#id2
requests文档:http://cn.python-requests.org/en/latest/user/quickstart.html
#get_postdata.py
from flask import Flask,request
app = Flask(__name__)
app.debug = True
@app.route('/',methods=['POST','GET']) #根据requests方法来确定method,即request.post()/request.get()来确定此处为get还是post
def get_data():
touser = request.args.get("ToUserName","") #取得字典中对应key的value值
print touser
return touser #将数据返回到对应的请求
if __name__ == "__main__":
app.run()
requests/flask框架中参数的传递
最新推荐文章于 2023-07-12 11:35:14 发布