Package: hehey-hclient
hehey-hclient 组件介绍hehey-hclient 是一个python 客户端请求工具组件,常用于接口的调用依赖以及版本要求python >= 3.5pycurl安装直接下载:命令安装：pip install hehey-hclient基础文件以目录参数配置# 所有配置conf={'customSites':{'xwsite':{'host':'http://api.xxx.cn/','method':'POST','response':{'clazz':'service','format':"json"}},'bcsite':{'transport':"socket",'clazz':'http','host':'http://api.xxx.cn/','method':'POST','response':{'clazz':'service','format':"json"}}}};# response 配置responseConf={'clazz':'request class name',# request 类名,比如http'host':'http://api.xxx.cn/',# 接口host 地址'format':'json',# 数据格式类型,'headers':{},# 默认header 信息'options':{},# 传输协议配置'method':'POST','response':{# resposne 配置'clazz':'service',# response 类'format':"json"# 数据格式类型,}}基本示例快速使用fromhclient.clientimportClient;hclient=Client()request=hclient.get('http://www.baidu.com/')response=request.send()# 获取response 返回的结果html=response.getContent()data=response.getData()发送HTTP,请求GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT,OPTIONSfromhclient.clientimportClient;hclient=Client()request=hclient.get('http://www.baidu.com/',{'user_id':2})response=request.send()# 获取response 返回的结果html=response.getContent()# 获取response 格式化后结果,比如jsondata=response.setFormat('json').getData();发送服务站点api 接口fromhclient.clientimportClient;hclient=Client({'customSites':{'xwsite':{'host':'http://api.xxxx.cn/','method':'POST','response':{'clazz':'service','format':"json"}},'bcsite':{'transport':"socket",'clazz':'http','host':'http://api.xxxx.cn/','method':'POST','response':{'clazz':'service','format':"json"}}}})# 方式1response=hclient.service('site1','user/getinfo',{'user_id':2}).send()# 获取response 结果data=response.getData();# 方式2response=hclient.bcsite.service('user/getinfo',{'user_id':2}).send();data=response.getData();批量发送请求# 方式1fromhclient.clientimportClient;hclient=Client()request1=hclient.get('http://www.baidu.com/')request2=hclient.get('http://www.baidu.com/')requests={'res1':request1,'res2':request2}responses=hclient.batchSend(requests);html1=responses['res1'].getContent()html2=responses['res2'].getContent()# 方式2fromhclient.clientimportClient;hclient=Client()requestGroup=hclient.batch()requestGroup.get('http://www.baidu.com/',index="res1")requestGroup.get('http://www.baidu.com/',index="res1")responses=requestGroup.send();html1=responses['res1'].getContent()html2=responses['res2'].getContent()即刻发送请求-直接获取结果fromhclient.clientimportClient;hclient=Client()html=hclient.getResult('http://www.baidu.com/')# 支持getResult,postResult 等等验证请求错误fromhclient.clientimportClient;hclient=Client()response=hclient.post('http://www.baidu.com/',{'user_id':2}).send();# 验证是否错误,验证网络,解析数据,Transport（传输层） 是否有错误ifresponse.hasError():# 请求错误passelse:# 请求正常pass# 验证是否网络错误,主要验证header http-code 状态码 是否等于20xifresponse.hasNetworkError():# 请求错误passelse:# 请求正常pass# 获取错误信息response.getErrors()# 获取全部错误response.getFirstError()# 获取首个错误信息其他方法介绍fromhclient.clientimportClient;hclient=Client()request=hclient.service('http://www.baidu.com/',{"id":1})request.setFormat("json")# 设置参数的格式request.addHeaders("Content-Type",'application/json; charset=UTF-8');# 设置http 头部信息request.setCookies({"name":1})# 设置cookieresponse=request.send()response.getStatusCode()# 获取请求状态码response.getHeaders()# 获取头部response.getHeaders()# 获取头部对象data=response.setFormat("json")# 设置返回内容的格式扩展# 自定义request,重写其方法fromhclient.base.RequestimportRequestclassHttpRequest(Request):defprepare(self):pass# 自定义Response,重写其方法fromhclient.base.ResponseimportResponseclassServiceResponse(Response):def__init__(self,attrs={}):self.varCode='code'self.varMsg='message'self.varResult='data'self.defaultCode=0self.errcode=''self.errmsg=''self._init=Falseifattrs:super().__init__(attrs)# 检查业务是否错误# <B> 说明： </B># <pre># 略# </pre>defcheck(self,errorCode=[]):self.__initData()ifnoterrorCode:errorCode=[self.defaultCode]ifself.errcodeinerrorCode:returnTrueelse:returnFalsedefgetCode(self):self.__initData()returnself.errcodedefgetMessage(self):self.__initData()returnself.errmsgdefgetResult(self):data=self.getData()result=data.get(self.varResult,None)returnresultdef__initData(self):ifself._init:returndata=self.getData()self.errmsg=data.get(self.varMsg,None)self.errcode=data.get(self.varCode,None)self._init=True# 对应的服务站点配置conf={'customSites':{'xwsite':{'clazz':'http',# 或hclient.protocol,HttpRequest.HttpRequest'host':'http://api.xxx.cn/','method':'POST','response':{'clazz':'service','format':"json"}},}}