• python requests 调用restful api


    #!/usr/bin/python
    # -*- coding: utf-8 -*-

    import json
    import requests
    from urlparse import urljoin

    BASE_URL = 'http://192.168.26.67:8000'
    AUTH = ('admin', 'admin')


    def test_get_user_list():
    rsp = requests.get(urljoin(BASE_URL, '/snippets/'), auth=AUTH, headers={
    'Accept': 'application/json'
    })
    return rsp


    def test_post_user_list():
    json_data = dict(
    title='zhangsan',
    code='oo',
    linenos='true'
    )
    rsp = requests.post(urljoin(BASE_URL, '/snippets/'), auth=AUTH, headers={
    'Accept': 'application/json',
    'Content-Type': 'application/json',
    }, data=json.dumps(json_data))
    return rsp

    def test_get_user():
    rsp = requests.get(urljoin(BASE_URL, '/snippets/17'), auth=AUTH, headers={
    'Accept': 'application/json',
    'Content-Type': 'application/json',
    })
    return rsp

    def test_put_user():
    json_data = dict(
    title='zhangsan',
    code='oo',
    linenos='true'
    )
    # 注意最后的 /
    rsp = requests.put(urljoin(BASE_URL, '/snippets/1/'), auth=AUTH, headers={
    'Accept': 'application/json',
    'Content-Type': 'application/json',
    }, data=json.dumps(json_data),
    )
    return rsp

    def test_patch_user():
    json_data = dict(
    title='aaaaaaaaaaaaaaaaaaaa',
    )
    rsp = requests.patch(urljoin(BASE_URL, '/snippets/1/'), auth=AUTH, headers={
    'Accept': 'application/json',
    'Content-Type': 'application/json',
    }, data=json.dumps(json_data),
    )
    return rsp

  • 相关阅读:
    elasticseacth基础操作--QueryBuilders的使用
    并发编程(八)并发安全
    redis集群 相关
    并发编程(七)线程池
    PMP--综合考试知识点,持续更新中。。。
    常用正则表达式
    测试计划
    ACC(Attribute Component Capability) 即特质,组件,能力
    LockScreen
    Custom Window
  • 原文地址:https://www.cnblogs.com/ExMan/p/10069641.html
Copyright © 2020-2023  润新知