Python接口自动化(二)接口开发

django

    • 配置开发环境
      • 相关命令
        • python manage.py runserver 127.0.0.1:8000
          在指定的地址和端口启动服务
        • python manage.py startapp api
          生成django项目应用目录
    • 开发接口流程
      • 在项目名文件夹下的urls.py添加映射的端口名和路径
      • 到views.py添加需要映射的方法
      • 启动项目后,前台展示结果
    • post请求开发
      • 先在templates下创建一个html新文件
      • 接着根据前端请求端口名login和请求方式POST进行判断
      • 打开页面后
        • 请求

          curl 'http://127.0.0.1:8000/login/' -H 'Connection: keep-alive' -H 'Cache-Control: max-age=0' -H 'Origin: http://127.0.0.1:8000' -H 'Upgrade-Insecure-Requests: 1' -H 'Content-Type: application/x-www-form-urlencoded' -H 'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3' -H 'Referer: http://127.0.0.1:8000/login/' -H 'Accept-Encoding: gzip, deflate, br' -H 'Accept-Language: zh-CN,zh;q=0.9' --data 'username=23124412&password=231321' --compressed
        • 返回
    • get
      • 根据前端login端口在地址栏中请求的参数判断
      • 打开页面请求后

        curl 'http://127.0.0.1:8000/login/?username=1111' -H 'Connection: keep-alive' -H 'Cache-Control: max-age=0' -H 'Upgrade-Insecure-Requests: 1' -H 'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3' -H 'Accept-Encoding: gzip, deflate, br' -H 'Accept-Language: zh-CN,zh;q=0.9' --compressed
    • 接口数据处理,返回json格式
      • get
        • 请求添加代码

        • curl 'http://127.0.0.1:8000/login/?username=1111&mobile=23123&data=32452342' -H 'Connection: keep-alive' -H 'Cache-Control: max-age=0' -H 'Upgrade-Insecure-Requests: 1' -H 'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3' -H 'Accept-Encoding: gzip, deflate, br' -H 'Accept-Language: zh-CN,zh;q=0.9' --compressed
      • post
        • 请求添加代码
        • 打开页面请求后

          curl 'http://127.0.0.1:8000/login/' -H 'Connection: keep-alive' -H 'Cache-Control: max-age=0' -H 'Upgrade-Insecure-Requests: 1' -H 'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36' -H 'Origin: http://127.0.0.1:8000' -H 'Content-Type: application/x-www-form-urlencoded' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3' -H 'Referer: http://127.0.0.1:8000/login/' -H 'Accept-Encoding: gzip, deflate, br' -H 'Accept-Language: zh-CN,zh;q=0.9' --data 'username=312312&password=231321' --compressed

posted on 2019-07-18 11:43  jiang1987  阅读(1181)  评论(0编辑  收藏  举报

导航