最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python下載文件的兩種方式

 更新時(shí)間:2024年08月07日 11:29:36   作者:碼農(nóng)研究僧  
這篇文章主要介紹了python下載文件的兩種方式:接口方式和Nginx這兩種方式,并通過(guò)代碼示例講解的非常詳細(xì),對(duì)大家學(xué)習(xí)python下載文件有一定的幫助,需要的朋友可以參考下

前言

Python開(kāi)發(fā)中時(shí)長(zhǎng)遇到要下載文件的情況,最常用的方法就是通過(guò)Http利用urllib或者urllib2模塊。

當(dāng)然你也可以利用ftplib從ftp站點(diǎn)下載文件。此外Python還提供了另外一種方法requests。

下面來(lái)看看三兩種方式是如何來(lái)下載文件的:

1. 接口方式

對(duì)于flask1.0的版本可以使用如下方式(通過(guò)接口)

from flask import Flask, send_file, abort

app = Flask(__name__)

@app.route('/download/<filename>')
def download_file(filename):
    try:
        # 文件路徑
        file_path = f'/path/to/your/files/{filename}'
        
        # 確保文件存在
        if not os.path.isfile(file_path):
            abort(404)  # 文件不存在,返回 404 錯(cuò)誤

        # 發(fā)送文件
        return send_file(file_path, as_attachment=True, attachment_filename=filename)
    except Exception as e:
        # 捕獲異常并返回 500 錯(cuò)誤
        return str(e), 500

if __name__ == '__main__':
    app.run(debug=True)

以上只是作為展示

如果是前后進(jìn)行交互,基本的Demo如下:(flask2.0版本)

from flask import Blueprint, render_template, send_file

bp = Blueprint('main', __name__)

@bp.route('/')
def index():
    return render_template('index.html')


@bp.route('/download')
def download():
    # 假設(shè)壓縮包文件路徑為 '/path/to/your/file.zip'
    file_path = '/root/xx.rar'
    return send_file(file_path, as_attachment=True, download_name='xx.rar')

對(duì)于前端的按鈕配置如下:

<button onclick="downloadFile()">下載壓縮包</button> <!-- 新增的下載按鈕 -->

后續(xù)只需要把對(duì)應(yīng)文件放置在相應(yīng)位置即可

截圖如下:

2. Nginx

總體配置如下:

server {
    listen 80;
    server_name ip地址;

    location / {
        proxy_pass http://127.0.0.1:5000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    location /downloads/ {
        alias /root/;
        autoindex on; # 啟用目錄索引(可選)
    }

    # Redirect HTTP to HTTPS
    location / {
        return 301 https://$host$request_uri;
    }
}

server {
    listen 443 ssl;
    server_name ip地址;

    ssl_certificate /etc/letsencrypt/live/your_domain/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/your_domain/privkey.pem;

    location / {
        proxy_pass http://127.0.0.1:5000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    location /downloads/ {
        alias /root/;
        autoindex on; # 啟用目錄索引(可選)
    }
}

請(qǐng)確保替換 /etc/letsencrypt/live/your_domain/fullchain.pem 和 /etc/letsencrypt/live/your_domain/privkey.pem 路徑為 Certbot 創(chuàng)建的證書路徑

到此這篇關(guān)于python下載文件的兩種方式的文章就介紹到這了,更多相關(guān)python下載文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

东方市| 五峰| 平度市| 苍南县| 丰原市| 长乐市| 乐都县| 绥棱县| 伊金霍洛旗| 鄂尔多斯市| 潍坊市| 崇义县| 从化市| 剑阁县| 河池市| 巧家县| 府谷县| 拉孜县| 历史| 青铜峡市| 壶关县| 平昌县| 清流县| 通渭县| 上蔡县| 沭阳县| 通道| 三明市| 塘沽区| 白玉县| 枞阳县| 天镇县| 军事| 桐庐县| 阳谷县| 宁德市| 河北省| 隆昌县| 类乌齐县| 灯塔市| 安顺市|