Python3(25):python request上传文件

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)

参考:

python request/读写/上传文件 - 简书

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

宁宁可可

您的鼓励是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值