• 使用python的接口测试环境搭建及使用


    环境搭建
    python 安装:建议使用python3.7
    pycharm安装
    requests安装 :pip3 install requests
    requests 基本使用
    usage:

    >>> import requests
    >>> r = requests.get('https://www.python.org')
    >>> r.status_code
    200
    >>> 'Python is a programming language' in r.content
    True

    ... or POST:

    >>> payload = dict(key1='value1', key2='value2')
    >>> r = requests.post('https://httpbin.org/post', data=payload)
    >>> print(r.text)
    {
    ...
    "form": {
    "key2": "value2",
    "key1": "value1"
    },
    ...
    }
    获取返回数据:
    res = requests.post(url,data).text  返回的是text格式
    res = requests.post(url,data).json  返回的是json格式
    requests上传文件的例子:
    import requests
    import json
    #上传文件
    #url = 'https://www.imooc.com/user/postpic'
    download_url = 'http://file.mukewang.com/imoocweb/webroot/mobile/imooc7.2.010102001android.apk'
    file = {
    "fileField":("test.jpg",open("E:/ytxu/test.jpg","rb"),"image/jpg"),
    "type":"1"
    }
    cookie = {
    "apsid":"I5ZTVmZmUzMGE1NDY2OTljZjFjYzkyMTMyMjk3MmUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAANzIxMzU2MQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABNdXNoaXNoaV94dUAxNjMuY29tAAAAAAAAAAAAAAAAADVjZDY5ZWYxMGQ2MmFlZDVmNTJkYWQ0ZWNhNjU5MjZhz%2BMFXc%2FjBV0%3DZW"
    }
    res = requests.get(download_url)
    with open("mukewang.apk","wb") as f:
    f.write(res.content)
    #res = requests.post(url,files=file,cookies=cookie,verify=False).json()
    print(res)
    #res = requests.post(url,files=file,cookies=cookie,verify=False).json()
    print(res)
    requests 下载apk文件
    requests 中 hearder介绍
    requests hearder加密
     
    flask环境搭建及简单开发
    接口开发:
    #coding=utf-8
    import requests
    import json

    from flask import Flask
    app = Flask(__name__)

    @app.route("/")
    def logine():
    data = json.dump(
    {
    'username': "aa",
    'password': "111111"
    }
    )

    return data
    if __name__ == "__main__":
    app.run()
  • 相关阅读:
    centos下搭建多项目svn服务器
    LAMP环境的搭建(三)----PHP7的安装
    LAMP环境的搭建(四)----Apache下部署项目
    LAMP环境的搭建(二)----Mysql安装与配置
    LAMP环境的搭建(一)----Apache安装
    shared memory segment exceeded your kernel's SHMMAX parameter
    无锁队列的实现
    CAS中的ABA问题
    MESI-CPU缓存一致性协议
    CAS lock-free
  • 原文地址:https://www.cnblogs.com/LinxiHuang/p/13191086.html
Copyright © 2020-2023  润新知