[TOC]
0x01 Flask 实践之编写简单 API 接口进行文件目录的同步
描述: 使用Flask我们可以非常方便的写出一个API请求接口或者网页, 此处我将利用该接口执行存在的shell脚本,利用rsync命令同步文件夹与文件,同时将结果回显到模板文件中。
Flask 主文件: setup.py
代码语言:javascript复制import subprocess
from flask import Flask
from flask import request,render_template
app = Flask(__name__)
def __external_cmd(cmd, code="utf-8"):
result = []
process = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
while process.poll() is None:
line = process.stdout.readline()
line = line.strip()
if line:
result.append(line.decode(code, 'ignore'))
return result
# def sh(command, print_msg=True):
# p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
# lines = []
# for line in iter(p.stdout.readline, b''):
# line = line.rstrip().decode('gbk', 'ignore')
# if print_msg:
# print(">>>", line)
# lines.append(line)
# return lines
@app.route('/')
@app.route('/index')
def index():
return "<h4 style='text-algin:center'>https://blog.weiyigeek.top</h4><script>window.location.href='https://blog.weiyigeek.top'</script>"
@app.route('/web/rsync',methods=["GET"])
def sync():
rsyncFlag = request.args.get("action")
if (rsyncFlag and rsyncFlag == "ok"):
res = __external_cmd("/usr/bin/rsync -auv --delete /work/web/ /work/OfficialWebsite/")
return render_template('index.html',command=res)
else:
return "<script>var r=confirm('是否进行上线同步?');if(r==true){window.location.href='/web/rsync?action=ok';}else{window.location.href='https://blog.weiyigeek.top';};</script>"
if __name__ == '__main__':
app.run(host='0.0.0.0',port=8000)
同步脚本: rsync.sh
代码语言:javascript复制#!/bin/bash
rsync -auv --delete /app/weiyigeekweb/weiyigeek.com.cn/ /app/weiyigeekweb/web/
模板文件: templatesindex.html
代码语言:javascript复制<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>网站同步状态回显</title>
<style>
.textarea{
width: 50%;
min-height: 20px;
margin-left: auto;
margin-right: auto;
padding: 3px;
outline: 0;
border: 1px solid #a0b3d6;
font-size: 12px;
line-height: 24px;
padding: 2px;
word-wrap: break-word;
overflow-x: hidden;
overflow-y: auto;
border-color: rgba(82, 168, 236, 0.8);
box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.1), 0 0 8px rgba(82, 168, 236, 0.6);
background-color: black;
color: white;
}
</style>
</head>
<body>
<div style="text-align: center;">
<h3>网站同步状态回显</h3>
<a href="https://www.weiyigeek.top" target="_blank">https://www.weiyigeek.top</a>
<br>
<br>
</div>
<div class="textarea" contenteditable="true">
{% for comment in command %}
{{ comment }} <br>
{% endfor %}
</div>
</body>
</html>
温馨提示: 构建出Flask运行环境的镜像
代码语言:javascript复制$ ls
Dockerfile requirements.txt
$ cat requirements.txt
Flask
$ cat Dockerfile
FROM python:slim-buster
LABEL maintainer="WeiyiGeek"
description="Flask Website Rsync"
version="v1.2"
WORKDIR /app
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
&& sed -i -e 's#http://deb.debian.org#https://mirrors.huaweicloud.com#g' -e 's|deb http:|#deb http:|g' /etc/apt/sources.list
&& apt update
&& apt install -y rsync
&& apt clean && apt-get autoclean && apt-get -y autoremove
EXPOSE 8000
CMD ["sleep","600"]
执行与运行效果
代码语言:javascript复制$ python setup.py
$ curl http://127.0.0.1:8000/web/rsync
s