• 接口开发 & 网络编程


    1.接口开发

    import flask
    import json
    import MySQL#《数据库造数据》博客中有此模块
    
    server = flask.Flask(__name__)
    
    
    @server.route('/api/user',methods=['post','get'])
    def user_info():
        d = {"id":3,"username":"huahua"}
        return json.dumps(d,ensure_ascii=False)
    
    #get
    @server.route('/get',methods=['get'])
    def user2():
        name = flask.request.values.get("name")
        data  = {"name":name}
        return json.dumps(data,ensure_ascii=False)
    
    #header,cookie
    @server.route('/header',methods=['get','post'])
    #这个接口是为了说明怎么获取cooke,获取header的
    def user3():
        name = flask.request.headers.get("name") #从header里面获取数据
        time = flask.request.cookies.get("time") #cookie获取数据
        data  = {"name":name,'time':time}
        return json.dumps(data)
    
    #json
    @server.route('/json',methods=['post'])
    def user4():
        if flask.request.is_json:
            age = flask.request.json.get("age")
            name = flask.request.json.get("name")
            sex = flask.request.json.get("sex")
            data = {"age":age,"sex":sex,"name":name}
        else:
            data = {"code":-1,"msg":"入参不是json"}
    
        return json.dumps(data,ensure_ascii=False)
    
    #文件上传
    @server.route('/file',methods=['post'])
    def user5():
        file = flask.request.files.get("file")
        file.save(file.filename)
        return json.dumps({"code":0,"msg":"上传成功"},ensure_ascii=False)
    
    #数据库查询
    #user、stu_hua
    #table_name=user
    #/table_data?table_name=
    @server.route('/table_data')
    def table_data():
        table_names = ['user','stu_hua']
        table_name = flask.request.values.get('table_name') #table_name=stu+salt
        # table_name = flask.request.values.get('sign') #sdgsdsdgsgds
        if table_name:
            if table_name in table_names:
                sql = 'select * from %s;' % table_name
                result = MySQL.execute_sql(sql)
                data = {"code":0,"msg":"成功","data":result}
                return json.dumps(data, ensure_ascii=False)
            else:
                return json.dumps({"code":-1,"msg":"没有查询该表的权限"},ensure_ascii=False)
    
        return json.dumps({"code":-1,"msg":"请传入table_name"},ensure_ascii=False)

    2.网络编程,调用接口

    import json
    from urllib.request import urlopen
    from urllib.parse import urlencode
    import requests
    
    #get请求
    url = "http://127.0.0.1:8888/get?name=abbb"
    req = urlopen(url)
    result = json.loads(req.read().decode())
    print(result)
    
    #post请求
    url = "http://127.0.0.1:8888/get"
    data = {"name":"post"}
    #name=xxx
    req = urlopen(url,urlencode(data).encode())
    print(req.read().decode())
    
    #get
    url = "http://127.0.0.1:8888/get"
    data = {"name":"哈哈哈哈"}
    req = requests.get(url=url,params=data)
    
    #post
    url = "http://127.0.0.1:8888/get"
    data = {"name":"哈哈哈哈"}
    req = requests.post(url,data=data,params={"xx":"xx"})
    # params=xx,参数是传在url后面的
    # data=xxx,参数是传在body里面的form-data的
    
    #header、cookie
    url = "http://127.0.0.1:8888/header"
    # req = requests.post(url,headers={"name":"xiaoming"},cookies={"time":"123456","time2":"666666"})
    req = requests.post(url,headers={"name":"xiaoming","cookie":"time=123456;time2=666666"})
    
    #入参是json的
    url = "http://127.0.0.1:8888/json"
    data = {"age":36,"name":"哈哈哈哈","sex":1111}
    req = requests.post(url,json=data)
    
    #文件上传
    url = "http://127.0.0.1:8888/file"
    files = {"file":open("/Users/nhy/Desktop/linux.emmx",'rb')}
    req = requests.post(url,files=files)
    
    #文件下载
    url = "https://q4.qlogo.cn/g?b=qq&nk=1357396742&s=140"
    req = requests.get(url)
    with open("lby.jpg",'wb') as fw:
        fw.write(req.content)
    
    #返回值类型转换
    print(req.status_code) #返回的http状态码
    print(req.json() ) #直接把返回的数据转成字典
    print(req.text) #返回的是字符串
    print(req.content) #返回的是bytes类型的
    print(req.cookies) #返回的cookie
    print(req.headers) #响应头
    加油
  • 相关阅读:
    使用Node.js创建第一个应用
    Node.js安装
    Node.js简介
    c#中的索引器
    ASP.NET常用内置对象(三)Server
    ASP.NET常用内置对象(二)Response
    ASP.NET常用内置对象(一)Request
    利用JDBC工具类 模拟用户登录!
    JDBC工具类完整版!
    用户登录
  • 原文地址:https://www.cnblogs.com/huahuacheng/p/14351007.html
Copyright © 2020-2023  润新知