初试Flask

#coding=utf-8

#服务器端代码  5
from flask import Flask,request,json

app = Flask(__name__)
@app.route('/success/<name>')
def success(name):
    return 'welcome %s'%name

@app.route('/login',methods = ['POST','GET','DELETE'])
def login():
    if request.method == 'POST':
        # return 'you are a post!'  #对应5.1

        # return '{"key": "I am a boy!!!"}' #对应5.2

        browser_type = request.headers.get("user-agent")#对应5.3
        return 'your header is :' + browser_type#对应5.3

    elif request.method == "DELETE":
        return "you are a delete!"
    else:
        return "you are a get!"
if __name__ == '__main__':
    app.run(debug = True)
#coding=utf-8

import requests
"启动5服务器,然后在启动5.1,可以查看服务器返回结果" \
"复杂的话可能是去数据库里进行操作,返回一些结果"

r = requests.get('http://127.0.0.1:5000/login')
print("get:",r.text)

r = requests.post('http://127.0.0.1:5000/login')
print("post:",r.text)

r = requests.delete('http://127.0.0.1:5000/login')
print("delete:",r.text)
#coding=utf-8

import requests,json
"启动5服务器,然后在启动5.2,可以查看服务器返回结果"
"复杂的话可能是去数据库里进行操作,返回一些结果"


r = requests.post('http://127.0.0.1:5000/login')

print(r.json()) #自动将返回的json串,转换成字典
print(type(r.json()))
#coding=utf-8

import requests,json
"启动5服务器,然后在启动5.3,可以查看服务器返回结果"
"复杂的话可能是去数据库里进行操作,返回一些结果"

url = 'http://127.0.0.1:5000/login'
headers = {'user-agent':'my-app/0.0.1'}
r = requests.post(url,headers=headers)
print(r.text)

 

posted @ 2020-12-11 14:08  不不田鼠  阅读(67)  评论(0编辑  收藏  举报