ftp功能的小网站(flask实现)

起因

家里有个退休的主机,打算拿它做ftp服务器,实现文件的同步。由于在局域网中,且该主机使用水晶头线连接,传输速率很快

准备

可以到Pycharm直接选择新建flask项目,没有的话输入下面的命令也是可以的。下载flask嫌慢的话可以配置一下镜像,详细查看这篇文章:pip镜像管理和npm镜像管理

pip install flask

新建flask项目

项目概述

项目结构

app.py是整个项目的"管家",负责监听还有逻辑处理

app.py文件:

from flask import Flask, render_template, request, send_from_directory, abort
import os

app = Flask(__name__)


def getfile():
    print(os.getcwd())
    path1 = os.getcwd() + '/upload_files'
    path2 = os.getcwd()
    # 跳转目录 跳转到下载文件的目录,获得下载文件目录里面的list之后,然后再跳转出来.
   #这个跳转的步骤是为了获取到upload_files目录下的文件的名字,然后把它放进f_list中
    os.chdir(path1)
    f_list = os.listdir()
    os.chdir(path2)
    print(os.getcwd())
    return f_list


def is_Have_file(filename):
    print(os.getcwd())
    path1 = os.getcwd() + '/upload_files'
    path2 = os.getcwd()
    os.chdir(path1)
    flag = os.path.isfile(filename)
    os.chdir(path2)
    print(os.getcwd())
    return flag


@app.route('/')
def home():
    return render_template('home.html')


@app.route('/upload')
def upload_file():
    return render_template('upload.html')


@app.route('/uploader', methods=['GET', 'POST'])
def upload_success():
    if request.method == 'POST':
        f = request.files['file']
        f.save('upload_files/' + f.filename)
        return render_template('upload_success.html', filename=f.filename)


# 显示下载文件的界面
@app.route('/down', methods=['GET'])
def download_page():
    f_list = getfile()
    return render_template('download_page.html', fl=f_list)


# 下载要下载的文件,要下载的文件是通过get方法来传递的
@app.route('/download_file', methods=['GET'])
def download_file():
    if request.method == 'GET':
        download_filename = request.args.get('filename')
        f_list = getfile()
        print()
        if is_Have_file(download_filename):
            return send_from_directory('upload_files', download_filename, as_attachment=True)
        else:
            abort(404)


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

首先看一下主页文件

home.html

home.html

<html>
<head>
    <title>首页</title>
    <style>
        body{
            background-color: antiquewhite;
            color: blueviolet;
            font-size: 30px;
            text-align: center;
        }

        #op{
            margin-top: 250px;
        }

        #title{
            font-size: 50px;
            margin-top: 50px;
            color: blue;
        }

        a{
            text-decoration: none;
        }
    </style>
</head>
    <p id="title">ftp功能小网站</p>
    <div id="op">
        <a href="{{ url_for('upload_file') }}">upload_file</a>
        <br>
        <a href="{{ url_for('download_page') }}">download_page</a>
    </div>
</html>

看一下upload.html

upload.html

<html>
<head>
    <title>上传文件</title>
    <style>
        body{
            background: aquamarine;
            color: blueviolet;
            font-size: 30px;
        }

        #title{
            font-size: 50px;
            color: blue;
            text-align: center;
            margin-top: 50px;
        }

        #div{
            margin-top: 200px;
            margin-left: 350px;
        }

        a{
            text-decoration: none;
        }
    </style>
</head>
<body>
    <p id="title">上传文件</p>
<div id="div">
    <form action="{{ url_for('upload_success')}}" method="POST"
          enctype="multipart/form-data">
        <input type="file" name="file"/>
        <input type="submit"/>
    </form>
     <a href="{{ url_for('home') }}">back home</a>
</div>
</body>
</html>

看一下upload_success.html

upload_success.html

<html>
<head>
    <title>添加成功</title>
    <style>
        body{
            color: blueviolet;
            background-color: aquamarine;
            text-align: center;
        }

        #op{
            margin-top: 280px;
        }
    </style>
</head>
    <div id="op">
        <p>文件 {{ filename }} 添加成功</p>
        <a href="{{ url_for('home') }}">home</a>
    </div>
</html>

最后看一下下载页面download_page.html

download_page.html

<!doctype html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <title>下载文件页面</title>
    <style>
        body{
            text-align: center;
            font-size: 30px;
            background: aquamarine;
        }

        a{
            text-decoration: none;
        }
    </style>
</head>
<body>
<div id="main">
    <h2>可供下载的文件</h2>
    <ul>
        {% for file in fl %}
{#            <li><a href="/download_file?filename={{ file }}">  {{ file }} </a></li>#}
            <li><a href="{{url_for('download_file', filename=file)}}">  {{ file }} </a></li>
        {% endfor %}
    </ul>
    <a href="{{ url_for('home') }}">home</a>
</div>
</body>
</html>

结语

这里的前端页面并不绚丽, 功能也非常简单,这个小网站只是为了我自己多台设备共享文件带来便利。感谢您观看。

猜你喜欢

转载自blog.csdn.net/weixin_43850253/article/details/107534090