国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Python(35):Python3 通過(guò)https上傳文件和下載文件

這篇具有很好參考價(jià)值的文章主要介紹了Python(35):Python3 通過(guò)https上傳文件和下載文件。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

Python(35):Python3 通過(guò)https上傳文件和下載文件

Python http方式的下載,參考:https://blog.csdn.net/fen_fen/article/details/113753983

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)!

本文來(lái)自互聯(lián)網(wǎng)用戶(hù)投稿,該文觀(guān)點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • Python學(xué)習(xí)筆記:Requests庫(kù)安裝、通過(guò)url下載文件

    Python學(xué)習(xí)筆記:Requests庫(kù)安裝、通過(guò)url下載文件

    在pipy或者github下載,通常是個(gè)zip,解壓縮后在路徑輸入cmd,并運(yùn)行以下代碼 ?安裝完成后,輸入python再輸入import requests得到可以判斷時(shí)候完成安裝 ?2.通過(guò)url下載文件 使用的是urllib模塊

    2024年02月10日
    瀏覽(86)
  • python flask創(chuàng)建服務(wù)器實(shí)現(xiàn)文件的上傳下載,已獲千贊

    python flask創(chuàng)建服務(wù)器實(shí)現(xiàn)文件的上傳下載,已獲千贊

    } 二、后端代碼(python) import os from string import Template from flask import Flask, send_file,request,jsonify from flask_cors import CORS import time app = Flask( name ) absolute = os.path.dirname( file ) CORS(app) app.debug = True FOLDER = os.path.join(absolute, ‘save_file’) HOST = “l(fā)ocalhost” PORT = 8080 UPLOAD = ‘upload’ app.config

    2024年04月22日
    瀏覽(103)
  • Python3 安裝后問(wèn)題(yum/https)

    問(wèn)題: 分析: 原 python命令軟連接指向linux默認(rèn)安裝的python2.x版本、 安裝python3時(shí)將原python命令的軟連接指向了python3 、 yum命令正常情況下調(diào)用python2.x 版本、所以需要修改yum命令文件 解決方案: 修改yum命令文件 第一行 原 #!/usr/bin/python 修改為 #!/usr/bin/python2.7 問(wèn)題: 分析:

    2023年04月26日
    瀏覽(22)
  • linux文件通過(guò)finalshell的上傳和下載

    linux文件通過(guò)finalshell的上傳和下載

    我們可以通過(guò)finalshell工具,方便的和虛擬機(jī)進(jìn)行數(shù)據(jù)交換 在finalshell下方的窗口中,提供了linux文件系統(tǒng)視圖: 瀏覽文件系統(tǒng),找到合適的文件,右鍵點(diǎn)擊下載,即可傳輸?shù)奖镜仉娔X 瀏覽文件系統(tǒng),找到合適的目錄,將本地電腦的文件拖拽進(jìn)入,即可方便的上傳數(shù)據(jù)到linux中

    2024年02月11日
    瀏覽(15)
  • python3ide手機(jī)安卓版下載,python3下載手機(jī)安卓版

    python3ide手機(jī)安卓版下載,python3下載手機(jī)安卓版

    大家好,小編來(lái)為大家解答以下問(wèn)題,python3ide手機(jī)安卓版下載,python3下載手機(jī)安卓版,今天讓我們一起來(lái)看看吧! 大家好,給大家分享一下python3ide安卓版官網(wǎng)下載,很多人還不知道這一點(diǎn)。下面詳細(xì)解釋一下Python中Turtle畫(huà)蝴蝶?,F(xiàn)在讓我們來(lái)看看! 在學(xué)習(xí)python的過(guò)程中,

    2024年04月24日
    瀏覽(22)
  • python3.9下載和安裝教程,python3.9下載pygame2.4.0

    python3.9下載和安裝教程,python3.9下載pygame2.4.0

    大家好,小編來(lái)為大家解答以下問(wèn)題,python3.9下載哪個(gè)版本的tensorflow,python3.9下載哪個(gè)版本pytorch,今天讓我們一起來(lái)看看吧! 目錄 一、Python 下載 二、Python 安裝 三、Python 配置 四、Python 驗(yàn)證 官網(wǎng)下載地址:Python Releases for Windows | Python.org (1)找到已經(jīng)下載好的 exe 安裝包

    2024年04月25日
    瀏覽(33)
  • C/C++ 通過(guò)HTTP實(shí)現(xiàn)文件上傳下載

    C/C++ 通過(guò)HTTP實(shí)現(xiàn)文件上傳下載

    WinInet(Windows Internet)是 Microsoft Windows 操作系統(tǒng)中的一個(gè) API 集,用于提供對(duì) Internet 相關(guān)功能的支持。它包括了一系列的函數(shù),使得 Windows 應(yīng)用程序能夠進(jìn)行網(wǎng)絡(luò)通信、處理 HTTP 請(qǐng)求、FTP 操作等。WinInet 提供了一套完整的網(wǎng)絡(luò)通信工具,使得開(kāi)發(fā)者能夠輕松地構(gòu)建支持網(wǎng)絡(luò)功

    2024年02月19日
    瀏覽(27)
  • 下載python3.10版本pycharm仍顯示python3.1以及官網(wǎng)如何下載舊版版本python

    下載python3.10版本pycharm仍顯示python3.1以及官網(wǎng)如何下載舊版版本python

    第二節(jié)?安裝Pycharm以及遇到的問(wèn)題 目錄 Day01新手小白學(xué)python 前言 一、pycharm下載安裝 二、遇到的問(wèn)題 1.下載的是python3.10版本仍顯示python3.1 has reached its end-od-life and is no longer supported? ????????2.官網(wǎng)如何下載舊版如3.9版本python ????????3.自定義安裝路徑 總結(jié) 安裝Pycha

    2024年02月06日
    瀏覽(32)
  • python3下載手機(jī)安卓版,python下載手機(jī)版最新

    python3下載手機(jī)安卓版,python下載手機(jī)版最新

    大家好,本文將圍繞python3下載手機(jī)安卓版展開(kāi)說(shuō)明,python下載手機(jī)版最新是一個(gè)很多人都想弄明白的事情,想搞清楚python手機(jī)版下載3.7.3需要先了解以下幾個(gè)事情。 1、先去python官網(wǎng)下載python3的源碼包,網(wǎng)址:https://www.python.org/ 1)進(jìn)去之后點(diǎn)擊導(dǎo)航欄的Downloads,出現(xiàn)python最新

    2024年01月21日
    瀏覽(18)
  • python3ide下載漢化破解版,python3ide下載手機(jī)安卓版

    python3ide下載漢化破解版,python3ide下載手機(jī)安卓版

    大家好,小編來(lái)為大家解答以下問(wèn)題,python3ide下載漢化破解版,python3ide下載手機(jī)安卓版,今天讓我們一起來(lái)看看吧! Source code download: 本文相關(guān)源碼 Pydroid3app是個(gè)強(qiáng)大好用的編程軟件,軟件使用起來(lái)比較方便,包含強(qiáng)大的編譯器,能夠幫助用戶(hù)在手機(jī)上輕松完成python編程,還

    2024年04月13日
    瀏覽(21)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包