Python 檔案共享伺服器
阿新 • • 發佈:2021-08-06
之前用Java寫過一個檔案系統,很簡陋,一次只能上傳一個檔案且無回顯,這次重新用Python寫了一個,因為只需要一個很小的服務,所以就選用Flask了
基於Flask的Web檔案共享
標籤:Python, Flask
之前用Java寫過一個檔案系統,很簡陋,一次只能上傳一個檔案且無回顯,這次重新用Python寫了一個,因為只需要一個很小的服務,所以就選用Flask了
效果如圖:
使用說明:
注:伺服器基於Flask,和Python,確保有相應環境
安裝flask命令: pip install flask
若是整個打包下載的,解壓後路徑下有個執行指令碼(執行.bat)雙擊執行就行了。檔案共享區域為此執行指令碼所在資料夾。也就是把檔案放入此資料夾,就可以被找到和下載,同樣的任意上傳的檔案也會被放到該目錄下。
上傳:拖到指定區域或者瀏覽本地選擇上傳均可
下載:點選檔案即可下載
打包下載
連結:https://pan.baidu.com/s/1MDX8iLjjaOSHqDtWoRMOZg
提取碼:kkkk
原始碼
程式碼量比較少,就沒傳github了
app.py
from flask import Flask, render_template, send_from_directory, request import os import socket from gevent import pywsgi app = Flask(__name__) @app.route('/') def index(): try: s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.connect(('8.8.8.8', 80)) ip = s.getsockname()[0] finally: s.close() path = os.getcwd() files = list(os.walk(path))[0][2] files.remove('執行.bat') return render_template('index.html', files=files, ip=ip) @app.route('/upload', methods=['POST']) def upload(): f = request.files['files'] f.save(os.path.join(os.getcwd(), f.filename)) return '{}' @app.route('/download/<filename>/') def download(filename): return send_from_directory(os.path.pardir, filename, as_attachment=True) if __name__ == '__main__': try: s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.connect(('8.8.8.8', 80)) ip = s.getsockname()[0] finally: s.close() print('複製' + ip + ':5000' + '到瀏覽器開啟') server = pywsgi.WSGIServer(('0.0.0.0', 5000), app) server.serve_forever()
index.html
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <title>檔案共享</title> <link rel="Shortcut Icon" href="/static/img/logo.ico" type="image/x-icon" /> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <link rel="stylesheet" href="/static/css/bootstrap.min.css"> <script src="/static/js/jquery.min.js"></script> <script src="/static/js/bootstrap.min.js"></script> <script src="/static/js/fileinput.min.js"></script> <link rel="stylesheet" href="/static/css/fileinput.min.css"> <script src="/static/js/zh.js"></script> </head> <body> <div class="container"> <h2>在同一區域網下輸入{{ip}}:5000即可訪問</h2> <br> <div class="list-group"> {% for file in files %} <a href="{{ url_for('download',filename=file)}}" class="list-group-item list-group-item-secondary">{{ file }}</a> {% endfor %} </div> <br> <div class="upload-wrap"> <input id="upload" type="file" multiple="multiple" name="files" /> </div> <script> $("#upload").fileinput({ language: 'zh', // 設定語言 uploadUrl: "{{url_for('upload')}}", // 上傳地址 uploadAsync: true, // 預設非同步上傳 showUpload: true, // 顯示上傳按鈕 showRemove: true, // 顯示移除按鈕 showPreview: true, // 顯示預覽 showCaption: true, // 顯示標題 browseClass: "btn btn-primary", // 按鈕樣式 dropZoneEnabled: true, // 顯示拖拽區域 maxFileCount: 30, // 允許同時上傳的最大檔案個數 enctype: 'multipart/form-data', validateInitialCount: true, previewFileIcon: "", msgFilesTooMany: "選擇上傳的檔案數量({n}) 超過允許的最大數值{m}!", }).on("fileuploaded", function (event, data, previewId, index) { window.alert('檔案上傳成功'); }); </script> </div> </body> </html>
待新增功能
- 支援手機掃二維碼訪問
- 支援刪除功能
- 支援瀏覽下載子目錄下的檔案