Python -- 使用requests模块发送GET和POST请求

①GET

# -*- coding:utf-8 -*-

import requests

def get(url, datas=None, **kwargs):
    response = requests.get(url, params=datas, **kwargs)
    json = response.json()
    return json

注:参数datas为json格式

实例

if __name__ == '__main__': 
    url = 'http://127.0.0.1:8080/test'
    datas = {
        'aaa' : 'bbb'
    }
    headers = {
        'Authorization' : 'xxxxxx'
    }
   
    ret = http_client.get(url, datas, headers=headers)
    print ret


②POST

# -*- coding:utf-8 -*-

import requests

def post(url, datas=None):
    response = requests.post(url, data=datas)
    json = response.json()
    return json
注:参数datas为json格式




你可能感兴趣的:(python)