import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
import json
# 封装HTTP GET请求方法
def get(**kwargs):
url = kwargs["protocol"] + "://"+kwargs["host"]+":"+str(kwargs["port"]) + kwargs["url"]
r = requests.get(url, headers=kwargs["headers"], verify=False)
r.encoding = 'UTF-8'
print(r.status_code)
return r.text
# 封装HTTP POST请求方法,支持上传图片
def post(files=None, **kwargs):
url = kwargs["protocol"] + "://" + kwargs["host"] + ':' + str(kwargs["port"])+ kwargs["url"]
if kwargs.get("data", "none") != "none":
data = json.dumps(kwargs["data"])
print(url)
r = requests.post(url, files=files, data=data, verify=False, headers=kwargs["headers"])
r.encoding = 'UTF-8'
json_response = r.text
print(r.status_code)
return json_response
调用:
def upload():
header = {
"Content-Disposition":'Content-Type="image/png";'}
files = {'uploadFile': open('d:\\1.PNG', 'rb')}
resp = configHttp.post(files=files, headers=header, url="/file/upload",
host="XXX", port=9090, protocol="http")
print("--upload--")
upID = (json.loads(resp)["data"]["resultList"][0]["upID"]) # 用于下载
print(upID)
doFileDownload( upID)
def queryGroupInfoByUserId(token):
header = {
"Content-Type": "application/json; charset=UTF-8",
"appId": "XXX", "secrectKey": "XXX=", "token": token}
data = {"WoUserId":"XXX"}
resp = configHttp.post(data=data, headers=header, url="/group/groupInfo/queryGroupInfoByUserId", host="XXX", port=8082, protocol="http")
print("--queryGroupInfoByUserId--")
print(resp)
def audit(token):
header = {
"Content-Type": "application/json; charset=UTF-8",
"secrectKey": "XXX=",
"appID": "XXX",
"token": token}
resp = configHttp.get(headers=header, url="/XXX/audit?auditId=XXXX",
host="XXX", port=8080, protocol="http")
print("--audit--")
print(resp)