码迷,mamicode.com
首页 > 编程语言 > 详细

【python接口自动化】httpUtils

时间:2018-06-04 16:34:37      阅读:538      评论:0      收藏:0      [点我收藏+]

标签:TE   自动   logger   util   sel   imp   get   body   put   

# coding=utf8
import requests
from common.logger import Logger
import logging

class httpUtils:
    logger = Logger("../logs/http.log",logging.INFO,logging.INFO)
    def get(self,url,params={},headers={}):
        res = requests.get(url,data=params,headers=headers)
        self.logger.info("请求路径:" + res.request.url)
        headerStr = ""
        for headerKey in res.request.headers:
            headerStr = headerStr + "\n" + headerKey + ":" + res.request.headers[headerKey]
        self.logger.info("请求头信息:" + headerStr)
        self.logger.info("请求参数:\n" + res.request.body)

        resHeaderStr = ""
        for resHeaderKey in res.headers:
            resHeaderStr = resHeaderStr + "\n" + resHeaderKey + ":" + res.headers[resHeaderKey]
        self.logger.info("响应头信息:" + resHeaderStr)
        self.logger.info("返回结果:\n" + res.content.decode("utf-8"))
        return res
    
    def post(self,url,params={},headers={}):
        res = requests.post(url,data=params,headers=headers)
        self.logger.info("请求路径:"+res.request.url)
        headerStr = ""
        for headerKey in res.request.headers:
            headerStr =headerStr+ "\n" + headerKey + ":" + res.request.headers[headerKey]
        self.logger.info("请求头信息:" + headerStr)
        self.logger.info("请求参数:\n"+res.request.body)

        resHeaderStr = ""
        for resHeaderKey in res.headers:
            resHeaderStr =resHeaderStr+ "\n" + resHeaderKey + ":" + res.headers[resHeaderKey]
        self.logger.info("响应头信息:"+resHeaderStr)
        self.logger.info("返回结果:\n"+res.content.decode("utf-8"))
        return res

 

【python接口自动化】httpUtils

标签:TE   自动   logger   util   sel   imp   get   body   put   

原文地址:https://www.cnblogs.com/zipon/p/9133317.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!