zoukankan      html  css  js  c++  java
  • 初试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)
  • 相关阅读:
    题解:luoguP1070 道路游戏(DP)
    题解:luoguP2577【ZJOI2005】午餐(DP)
    题解:bzoj1801: [Ahoi2009]chess 中国象棋
    题解:bzoj1878: [SDOI2009]HH的项链
    SpringBoot静态资源文件 lion
    简要的Log4Net 应用配置
    Web Service 初级教程
    log4Net 动态改变写入文件
    Ornament 类型资源权限
    JQuery 引发两次$(document).ready事件
  • 原文地址:https://www.cnblogs.com/bubutianshu/p/14120072.html
Copyright © 2011-2022 走看看