#!/usr/bin/python
# -*- coding: UTF-8 -*-
from requests import api
from fake_useragent import UserAgent
import json


def json_data(data):
    data2 = json.loads(data)
    data3 = json.dumps(obj=data2, indent=2, ensure_ascii=False)
    return data3


def print_resp(resp):
    print("------------------------------------")
    print("请求方法", resp.request.method)
    print("请求头", resp.request.headers)
    print("请求体\n", json_data(resp.request.body))
    print("请求URL", resp.request.path_url)
    print("响应URL", resp.url)
    print("状态码", resp.status_code)
    print("响应头", resp.headers)
    print("响应体\n", json_data(resp.text))
    # print("响应体",resp.content)
    print("重定向", resp.is_redirect)
    print("网页编码", resp.apparent_encoding)
    print("cookie", resp.cookies.values())
    print("------------------------------------")


def ok(resp):
    if resp.ok:
        print("请求成功")
        print_resp(resp)
    else:
        print("请求失败")
        print_resp(resp)


def header_random():
    header = {
        "user-agent": UserAgent().random,
        "Access-Control-Allow-Origin": "*",
        "Access-Control-Allow-Methods": "POST, GET, OPTIONS, DELETE, PUT",
        "Access-Control-Max-Age": "3600",
        "Access-Control-Allow-Headers": "x-requested-with"
    }
    return header


def get_func(url: str, params=None):
    resp = api.get(url=url, params=params, headers=header_random())
    ok(resp)


# application/x-www-form-urlencoded
def post_func(url: str, data):
    resp = api.post(url=url, data=data, headers=header_random())
    ok(resp)


# application/json
def post_json(url: str, json):
    resp = api.post(url=url, json=json, headers=header_random())
    ok(resp)


# application/x-www-form-urlencoded
def put_func(url: str, data):
    api.put(url, data=data, headers=header_random())


def put_json(url: str, data):
    api.put(url, json=data, headers=header_random())


def delete_fun(url: str):
    api.delete(url=url)



if __name__ == '__main__':
    # get_func("http://localhost:9001/doc.html#/home", params={"user": "kong", "pass": "1234"})
    post_json("http://localhost:9001/api/v1/channel/list", json={
        "page": 0,
        "size": 2,
    })
    # post_func("http://localhost:9001/doc.html#/home", data={"user": "kong", "pass": "1234"})
    put_json("http://localhost:9000/service_9001/api/v1/sensitive/update",data={id: 3104, 'sensitives': "666"})

import urllib3
import json

header = {
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
                  "Chrome/89.0.4389.90 Safari/537.36 Edg/89.0.774.63",
    "Access-Control-Allow-Origin": "*",
    "Access-Control-Allow-Methods": "POST, GET, OPTIONS, DELETE, PUT",
    "Access-Control-Max-Age": "3600",
    "Access-Control-Allow-Headers": "x-requested-with"
}
http = urllib3.PoolManager(headers=header)


def print_resp(resp):
    print("请求URL", resp._request_url)
    print("响应状态码", resp.status)
    data1 = resp.data.decode('utf-8')
    data2 = json.loads(data1)
    data3 = json.dumps(obj=data2, indent=2, ensure_ascii=False)
    print("响应数据\n", data3)


def get_method(url):
    resp = http.request("GET", url=url)
    print_resp(resp)


def post_method(url, data):
    resp = http.request("POST", url=url, fields=data)
    print_resp(resp)


def post_json(url, data):
    resp = http.request("POST", url=url, body=json.dumps(data, ensure_ascii=False),
                        headers={"Content-Type": "application/json;charset=UTF-8"})
    print_resp(resp)


def put_method(url, data):
    resp = http.request("PUT", url=url, fields=data)
    print_resp(resp)


def put_json(url, data):
    resp = http.request("PUT", url=url, body=json.dumps(data, ensure_ascii=False).encode('utf-8'),
                        headers={"Content-Type": "application/json;charset=UTF-8"})
    print_resp(resp)


def delete_method(url):
    resp = http.request("DELETE", url=url)
    print_resp(resp)




if __name__ == '__main__':
    # get_method("https://blog.csdn.net/")
    # post_method("http://localhost:9001/api/v1/channel/list", data={
    #     "page": 0,
    #     "size": 2,
    # })
    # data = {"page": 0, "size": 2}
    # post_json("http://localhost:9001/api/v1/channel/list/", data=data)
    #put_method("http://localhost:9000/service_9001/api/v1/sensitive/update",{id: 3104, 'sensitives': "666"})
    put_json("http://localhost:9000/service_9001/api/v1/sensitive/update",data={'id': 3104, 'sensitives': "666"})
    delete_method("http://localhost:9000/service_9001/api/v1/sensitive/del/1")
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐