httpUtils python

Posted lshan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了httpUtils python相关的知识,希望对你有一定的参考价值。

依赖:

pip install requests 

 

#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)
     
        
        print("请求路径:" + res.request.url)
        headerStr = ""
        for headerKey in res.request.headers:
            headerStr = headerStr + "
" + headerKey + ":" + res.request.headers[headerKey]
        print("请求头信息:" + headerStr)
        print("请求参数:
" + res.request.body)

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

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

 

 

使用:

 

#encoding=utf-8
‘‘‘
Created on 2019??10??9??
@author: sea
‘‘‘
# from com.sea.hhtp.MyHttp import get, post
from com.sea.hhtp import httputils



print("###################################################")
print("###################################################")
print("################       GET     ####################")
print("###################################################")
print("###################################################")
headers = { 
            "user-agent"  : "ad", 
            "Appstore-clientType" : "android", 
            "Appstore-IMEI" : "123456789000000"
            } 

getheader={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"}
getresult =httputils.get("http://192.168.18.129:7016/worktable?page=1&size=2")
print(str(getresult))
print("13213")
print("13213")




print("###################################################")
print("###################################################")
print("################       POST     ###################")
print("###################################################")
print("###################################################")



postresult = httputils.post("http://192.168.18.129:7016/worktable/dynamicQueryWithPage",{"status":"CCD","page":1,"size":2},{"Content-Type":"application/json"})
print(str(postresult))

 

 

 

 

 

使用:

以上是关于httpUtils python的主要内容,如果未能解决你的问题,请参考以下文章

HttpUtils 封装类

HttpUtils——OkHttp版本

asp及asp.net的urlencode问题

常用python日期日志获取内容循环的代码片段

python 有用的Python代码片段

Python 向 Postman 请求代码片段