Python(35):Python3 通過(guò)https上傳文件和下載文件
Python http方式的下載,參考:https://blog.csdn.net/fen_fen/article/details/113753983文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-816567.html
https需要先安裝需要的模塊文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-816567.html
1、上傳示例
1.1、調(diào)用:
upload_strategy(access_token,"123456789")
1.2、上傳代碼
global pkcs12_filename, pkcs12_password
pkcs12_filename = './conf/xxx-client-cert-xxx.p12'
pkcs12_password = 'xxx'
self.host=10.1.1.101
# 上傳文件
def upload_file(self, access_token,appid):
#warnings.simplefilter('ignore', ResourceWarning)
# filepath = os.getcwd()
# print(filepath)
# print(filepath+"\data\\"+fileName)
try:
header_up = {"Authorization": "Bearer " + access_token}
file=open("./data/"+fileName, "rb")
files={"file": file}
#files = {"file": open("./data/"+fileName, "rb")}
url = "https://"+self.host+"/xxx/v1/xxx/strategy/xxx?appId="+appid
api_json = base_request.upload_request(url, header_up, files)
if api_json["code"] == 0:
print(">>上傳策略成功。")
else:
print(">>上傳策略失敗,", api_json)
file.close()
return api_json
except Exception as e:
print(">>上傳策略失敗,Exception:", e)
# 基礎(chǔ)請(qǐng)求:上傳
def upload_request(url, headers, files):
response = requests_pkcs12.post(url, headers=headers, files=files,
pkcs12_filename=pkcs12_filename,
pkcs12_password=pkcs12_password, verify=False)
api_json = response.json() # 獲取請(qǐng)求返回實(shí)際結(jié)果的json串值
return api_json
2、下載示例
?2.1、下載調(diào)用:
self.host=10.1.1.101
# 下載客戶(hù)端
def download_client(self, header_all):
try:
url = "https://" + self.host + "/xxx/v1/xxx/download-client"
getfile = Getfile(url, header_all)
filename = getfile.getfilename()
#print(filename)
if filename:
getfile.download(filename)
except Exception as e:
print(">>下載客戶(hù)端失敗,Exception: ", e)
header_all = {"Content-Type": "application/json;charset=UTF-8"}
header_all['Authorization'] = "Bearer " + access_token
download_client(header_all)
2.2、Python3 https下載文件工具類(lèi):
# -*- coding: utf-8 -*-
"""
Create by HMF on 2024/01/11.
"""
import re,time
import requests
import requests_pkcs12
class Getfile(object): # 下載文件
global pkcs12_filename, pkcs12_password
pkcs12_filename = './conf/xxx-client-cert-xxx.p12'
pkcs12_password = 'xxx'
def __init__(self, url, headers):
self.url = url
self.headers = headers
def getheaders(self):
try:
#r = requests_pkcs12.head(self.url, headers=self.headers, verify=False)
r = requests_pkcs12.head(self.url, headers=self.headers, pkcs12_filename=pkcs12_filename, pkcs12_password=pkcs12_password, verify=False)
rs_headers = r.headers
return rs_headers
except Exception as e:
print(e)
# 獲取默認(rèn)下載文件名
def getfilename(self):
try:
if 'Content-Disposition' in self.getheaders():
file = self.getheaders().get('Content-Disposition')
# filename = re.findall('filename="(.*)"',file)
filename = re.split("=", file)[1]
if filename:
print("下載文件名:" + filename)
return filename
else:
print("下載文件失敗")
except Exception as e:
print(e)
# 下載文件
def download(self, filename):
try:
#self.r = requests_pkcs12.get(self.url, headers=self.headers, stream=True, verify=False)
self.r =requests_pkcs12.get(self.url, headers=self.headers,pkcs12_filename=pkcs12_filename,
pkcs12_password=pkcs12_password, stream=True, verify=False)
with open(filename, "wb") as code:
# 邊下載邊存硬盤(pán)
for chunk in self.r.iter_content(chunk_size=1024):
if chunk:
code.write(chunk)
time.sleep(1)
except Exception as e:
print(e)
到了這里,關(guān)于Python(35):Python3 通過(guò)https上傳文件和下載文件的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!