使用Python快速搭建文件傳輸服務(wù)的方法
當(dāng)我的朋友需要把他電腦上面的文件從他的電腦傳遞到我電腦上的時(shí)候,我只需要啟動(dòng)服務(wù)
啟動(dòng)服務(wù)!
他打開web界面
就能把文件傳遞到我電腦上(還能夠?qū)崟r(shí)顯示進(jìn)度)
文件就已經(jīng)在我電腦上的uploads文件夾里面了
項(xiàng)目結(jié)構(gòu)如下
templates 存放前端html文件
? updoad.html 上傳文件的界面
uploads 存放用戶上傳的文件
? 保研準(zhǔn)備資料.zip 剛剛上傳的文件
upload.py 后端服務(wù)文件
當(dāng)你和朋友在同一個(gè)局域網(wǎng)內(nèi),當(dāng)然可以直接根據(jù)主機(jī)的ip遠(yuǎn)程傳輸。當(dāng)你們兩個(gè)不在同一個(gè)網(wǎng)絡(luò)內(nèi)的時(shí)候,可以用frp內(nèi)網(wǎng)穿透將一臺(tái)主機(jī)的ip變成公網(wǎng)ip,也能實(shí)時(shí)進(jìn)行傳輸了。
優(yōu)點(diǎn):
- 簡(jiǎn)單快速,不需要打開qq,微信等軟件傳輸
- 沒有文件大小限制
- 進(jìn)度實(shí)時(shí)顯示
- 局域網(wǎng)也不需要聯(lián)網(wǎng),也非??焖伲?/li>
后端服務(wù)搭建
用flask來搭建web服務(wù)
from flask import Flask, render_template, request, jsonify import os app = Flask(__name__) UPLOAD_FOLDER = 'uploads' # 上傳文件保存的目錄 app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER @app.route('/') def index(): return render_template('upload.html') @app.route('/upload', methods=['POST']) def upload(): file = request.files['file'] if file: filename = file.filename file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename) file.save(file_path) return '文件上傳成功!' @app.route('/progress', methods=['POST']) def progress(): uploaded_bytes = request.form['uploadedBytes'] total_bytes = request.form['totalBytes'] progress = int(uploaded_bytes) / int(total_bytes) * 100 return jsonify(progress=progress) if __name__ == '__main__': app.run(debug=True)
只需要定義三個(gè)接口
- / : 默認(rèn)訪問html頁面,來供用戶操作
- /upload :上傳文件的post接口
- /progress : 實(shí)時(shí)顯示進(jìn)度的post接口
前臺(tái)頁面撰寫
<!doctype html> <html> <head> <title>文件上傳</title> <script> function uploadFile() { var fileInput = document.getElementById('file'); var file = fileInput.files[0]; var xhr = new XMLHttpRequest(); xhr.open('POST', '/upload'); xhr.upload.onprogress = function(event) { if (event.lengthComputable) { var progress = Math.round((event.loaded / event.total) * 100); document.getElementById('progress').innerText = progress + '%'; } }; xhr.onload = function() { if (xhr.status === 200) { document.getElementById('progress').innerText = '上傳完成'; } }; var formData = new FormData(); formData.append('file', file); xhr.send(formData); } function updateProgress() { var xhr = new XMLHttpRequest(); xhr.open('POST', '/progress'); xhr.onload = function() { if (xhr.status === 200) { var progress = JSON.parse(xhr.responseText).progress; document.getElementById('progress').innerText = progress + '%'; } }; xhr.send(); } setInterval(updateProgress, 1000); // 每秒更新一次進(jìn)度 </script> </head> <body> <h1>文件上傳</h1> <input type="file" id="file"> <button onclick="uploadFile()">上傳</button> <div id="progress"></div> </body> </html>
只需要執(zhí)行兩個(gè)函數(shù)就行
- onload() : 文件上傳函數(shù),調(diào)用后臺(tái)的 upload 上傳文件接口
- updateProgress() : 定時(shí)訪問后臺(tái)顯示進(jìn)度的 progress 接口,來獲取文件上傳的進(jìn)度,進(jìn)度計(jì)算后展示百分比給用戶
這樣任何一個(gè)人都能打開瀏覽器把他電腦上的文件傳給我了。
到此這篇關(guān)于使用Python快速搭建一個(gè)文件傳輸服務(wù)的文章就介紹到這了,更多相關(guān)Python搭建文件傳輸服務(wù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
TensorFlow2基本操作之合并分割與統(tǒng)計(jì)
這篇文章主要介紹了TensorFlow2基本操作之合并分割與統(tǒng)計(jì),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09基于Python實(shí)現(xiàn)虛假評(píng)論檢測(cè)可視化系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了如何基于Python實(shí)現(xiàn)一個(gè)簡(jiǎn)單的虛假評(píng)論檢測(cè)可視化系統(tǒng),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-04-04python創(chuàng)建屬于自己的單詞詞庫 便于背單詞
這篇文章主要為大家詳細(xì)介紹了python創(chuàng)建屬于自己的單詞詞庫,便于背單詞,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07Pytorch對(duì)Himmelblau函數(shù)的優(yōu)化詳解
今天小編就為大家分享一篇Pytorch對(duì)Himmelblau函數(shù)的優(yōu)化詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-02-02pygame實(shí)現(xiàn)俄羅斯方塊游戲(AI篇2)
這篇文章主要為大家詳細(xì)介紹了pygame實(shí)現(xiàn)俄羅斯方塊游戲AI的第2篇,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10python破解WiFi教程代碼,Python蹭網(wǎng)原理講解
用Python生成一個(gè)簡(jiǎn)單的密碼本,一般是有數(shù)字、字母和符號(hào)組成,這里用到的思路主要是窮舉法。通過使用pywifi?模塊,根據(jù)密碼本暴力破解WiFi。本文只是從技術(shù)的角度來闡述學(xué)習(xí)Pywifi庫!并不建議大家做任何破壞性的操作和任何不當(dāng)?shù)男袨椋?/div> 2023-01-01最新評(píng)論