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)文章
Pytorch?linear?多維輸入的參數(shù)問(wèn)題
這篇文章主要介紹了Pytorch?linear多維輸入的參數(shù)的問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08
Python序列排序的具體場(chǎng)景實(shí)現(xiàn)
本文主要介紹了Python序列排序的具體場(chǎng)景實(shí)現(xiàn),主要介紹了內(nèi)置的sort()方法或者全局的sorted()方法著兩種方法,具有一定的參考價(jià)值,感興趣的可以了解一下2025-01-01
python中正則表達(dá)式findall的用法實(shí)例
在寫著自動(dòng)化測(cè)試的腳本時(shí)重新復(fù)習(xí)了一下正則表達(dá)式findall()方法,下面這篇文章主要給大家介紹了關(guān)于python中正則表達(dá)式findall用法的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09
使用python處理一萬(wàn)份word表格簡(jiǎn)歷操作
這篇文章主要介紹了使用python處理一萬(wàn)份word表格簡(jiǎn)歷操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03
python argparse模塊通過(guò)后臺(tái)傳遞參數(shù)實(shí)例
這篇文章主要介紹了python argparse模塊通過(guò)后臺(tái)傳遞參數(shù)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04
Jupyter Notebook/VSCode導(dǎo)出PDF中文不顯示的解決
這篇文章主要介紹了Jupyter Notebook/VSCode導(dǎo)出PDF中文不顯示的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
python中使用urllib2獲取http請(qǐng)求狀態(tài)碼的代碼例子
這篇文章主要介紹了python中使用urllib2獲取http請(qǐng)求狀態(tài)碼的代碼例子,需要的朋友可以參考下2014-07-07
pip3一鍵卸載當(dāng)前環(huán)境中所有已安裝Python包(Linux/macOS/Windows)的完整教學(xué)
在 Python 開(kāi)發(fā)過(guò)程中,隨著不斷安裝和測(cè)試各種庫(kù),環(huán)境很容易變得臃腫混亂,本文將介紹一種通用安全可控的方法,適用于Linux/macOS和Windows,有需要的小伙伴可以了解下2026-01-01
win7上python2.7連接mysql數(shù)據(jù)庫(kù)的方法
這篇文章主要介紹了win7上python2.7連接mysql數(shù)據(jù)庫(kù)的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-01-01

