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)
  • 相关阅读:
    代码审查如何做
    使用 Gitbook 打造你的电子书
    Ztree的简单使用和后台交互的写法(一)
    ORACLE 错误:oralce record is locked by another user
    easyUI增加视图分组的办法
    jquery 解析数据库中的json日期为正常的格式
    Bootstrap基本类和组件学习二
    bootstrap的学习-基础样式和排版一
    easyUI中tree的简单使用
    orancle的安装和配置
  • 原文地址:https://www.cnblogs.com/bubutianshu/p/14120072.html
Copyright © 2011-2022 走看看