第一天,发送请求

第一天任务,封装请求方法
http_request.py

# -*- encoding: utf-8 -*-
"""
@File    : http_request.py
@Time    : 2022/11/7 15:41
@Author  : swithin
@Software: PyCharm
"""
import requests


class HttpRequest:
    """

    :param url:
    :param param:
    :param method:
    :return:
    """

    def http_request(self, url, method=None, params=None, data=None, json=None, headers=None):

        if method.lower() == "get":
            try:
                res = requests.get(url=url, params=params, headers=headers)
            except Exception as e:
                print("get request fail,error is :{0}".format(e))
        elif method.lower() == "post":
            try:
                res = requests.post(url=url, data=data, json=json, headers=headers, )
            except Exception as e:
                print("post request fail,error is :{0}".format(e))

        elif method.lower() == "put":
            try:
                res = requests.put(url=url, data=data, json=json, headers=headers)
            except Exception as e:
                print("put request fail,error is :{0}".format(e))
        elif method.lower() == "delete":
            try:
                res = requests.delete(url=url, data=data, json=json, headers=headers)
            except Exception as e:
                print("delete request fail,error is :{0}".format(e))
        else:
            print("method undefined")
        return res


if __name__ == "__main__":
    res = HttpRequest()
    url = "http://127.0.0.1/login"
    param = {
        "username": "admin",
        "password": "123456",
        "uuid": "360bf1a4cf9e46f387521fca331a6c4f"
    }
    method = "post"
    res = res.http_request(url, method=method, json=param)
    print(res.text)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值