Python3(25):python request上传文件
上传文件的主要几点:
url_upload="https://"+self.host+"/xxx/xxx/datasource-collect/upload" header = {"Authorization": "Bearer "+ accessToken } print (header) files={"file":open(".\data\902646-MYSQL-mytest-15-202006151235.json", "rb")} response_upload = requests.post(url_upload, files=files,headers=header,verify=False) apijson_upload = response_upload.json() # 获取请求返回实际结果的json串值
代码参考:
Create by HMF on 2021/02/10,2021/05/18
"""
import requests
import json
from datetime import datetime
class Casb(object): #登录
def __init__(self,host):
self.host=host
def login(self):
try:
requests.packages.urllib3.disable_warnings()
header = {"Content-Type": "application/json"}
data = {"username": "YWRtaW4=", "password": "bGlh"}
url = "https://"+self.host+"/xxx/login?random=1612094645702"
response = requests.post(url, data=json.dumps(data), headers=header, verify=False)
apijson_login = response.json() # 获取请求返回实际结果的json串值
#print(apijson_login)
if (apijson_login["code"]==0):
print ("管理平台登录成功")
else:
print ("管理平台登录失败",apijson_login)
return apijson_login
except Exception as e:
print("登录失败,Exception:",e)
def uploadCollect(self, accessToken):
try:
url_upload="https://"+self.host+"/xxx/datasource-collect/upload"
header = {"Authorization": "Bearer "+ accessToken }
print (header)
files={"file":open(".\data\902646-MYSQL-mytest-15-202006151235.json", "rb")}
response_upload = requests.post(url_upload, files=files,headers=header,verify=False)
apijson_upload = response_upload.json() # 获取请求返回实际结果的json串值
if (apijson_upload["code"]==0):
print ("上传表结构成功",apijson_upload)
else:
print ("上传表结构失败",apijson_upload)
return apijson_upload
except Exception as e:
print("上传表结构失败,Exception:", e)
host="10.1.1.181"
casb=Casb(host)
apijson_login =casb.login()
print(apijson_login)
accessToken = apijson_login["data"]["accessToken"]
response_aa=casb.uploadCollect(accessToken)
print (response_aa)
参考: