flask-bt之tracker录入
templates\trackers_index.html
-
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>trackers</title>
</head>
<body>
{% if session.logged_in %}
<form action="/logout" method="get">
<input type="submit" value="登出">
</form>
{% else %}
<form action="/login" method="post">
<input type="text" name="username" placeholder="用户名">
<input type="password" name="password" id="password" placeholder="密码">
<input type="submit" value="登录">
</form>
{% endif %}
<main>
<!-- flask的flash函数专用固定代码 专门用来显示内容的 -->
{% for message in get_flashed_messages() %}
<div class="alert">{{ message }}</div>
{% endfor %}
</main>
<p>tracker总数量:{{count}}</p>
<form action="/trackers_search" method="post">
<input type="text" name="trackers_name" placeholder="请输入tracker关键字">
<input type="submit" value="搜索">
</form>
{% if session.logged_in %}
<!-- 登录表单,实际上用户已经登录,所以可以显示欢迎信息或其他内容 -->
<form action="/trackers_add" method="post">
<label for="trackers_name">trackers_add:</label>
<input type="text" name="trackers_name" placeholder="请输入完整的tracker链接">
<input type="submit" value="提交">
</form>
<form action="/trackers_delete" method="post">
<label for="trackers_name">trackers_delete:</label>
<input type="text" name="trackers_name" placeholder="请输入完整的tracker链接">
<input type="submit" value="删除">
</form>
{% endif %}
<hr>
<div class="tracker_box scrollable-div">
<form action="/get_trackers_as_text" method="get">
<input type="submit" value="去复制,请使用Ctrl+A和Ctrl+C复制">
</form>
{% for i in trackers %}
<ul style="list-style-type: none">
<li>{{i.name}}</li>
</ul>
{% endfor %}
</div>
</body>
</html>
<style>
* {
margin:0;
padding:0;
}
.scrollable-div {
height: 800px; /* 或者你需要的高度 */
overflow-y: auto; /* 内容超出时在 y 轴方向显示滚动条 */
overflow-x: auto; /* 内容超出时在 x 轴方向显示滚动条 */
}
</style>
-
app.py
-
from flask import Flask, request, render_template, redirect, url_for, flash, session
from flask_sqlalchemy import SQLAlchemy
import os
from datetime import datetime
from flask_cors import CORS
import secrets
'''
待实现:
flash错误提示显示红色,警告信息显示黄色,成功显示绿色,使用div画出来
'''
app = Flask(__name__)
# 为整个应用启用 CORS
CORS(app)
# 启用 CORS 并设置特定的域名
#CORS(app, resources={r"/some_route": {"origins": "http://example.com"}})
# 或者,允许所有域名访问所有路由
CORS(app, resources={r"*": {"origins": "*"}})
# 获取当前文件的路径
basedir = os.path.abspath(os.path.dirname(__file__))
# 设置数据库文件的路径为当前目录下的 data.db
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'data.db')
#app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////data.db'
app.secret_key = secrets.token_urlsafe() # 用于保持会话安全,字符串随机即可
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False#模型修改的跟踪
db = SQLAlchemy(app)
class Trackers(db.Model):
__tablename__= 'trackers'
id = db.Column(db.Integer, unique=True, comment='id')
name = db.Column(db.String(64), primary_key=True, comment='Trackers,主键')
is_usable = db.Column(db.String(64), unique=False, comment='Tracker是否可用')
create_date = db.Column(db.DateTime, unique=False, default=datetime.now(), comment='Tracker创建日期')
update_date = db.Column(db.DateTime, unique=False, comment='Tracker更新日期')
def __repr__(self):
return '<Trackers %r>' % self.name
def create_tables():
with app.app_context():
db.create_all()
@app.route('/')
@app.route('/trackers_index')
def trackers_index():
trackers = Trackers.query.order_by(Trackers.create_date.desc()).all()#倒序输出所有
count = Trackers.query.count()#tracker总数量
return render_template('trackers_index.html', trackers=trackers, count=count)
@app.route('/get_trackers_as_text')
def get_trackers_as_text():
trackers = Trackers.query.all()
lines = [f"{tracker.name}" for tracker in trackers]
text = "<br>".join(lines)
return text
@app.route('/trackers_add', methods=['POST'], endpoint='add_announcement')
def announcement_add():
trackers_name = request.form['trackers_name']
# 关键词列表
keywords = ['announce', 'http', 'udp', 'https', 'wss']
# 检查字符串是否包含列表中的任意关键词
if contains_keywords(trackers_name, keywords) or trackers_name.endswith('announce'):
print("提交信息中包含关键字")
else:
flash("你提交的可能不是tracker链接")
return redirect(url_for('trackers_index'))
exists = Trackers.query.filter_by(name=trackers_name).first() is not None
if exists:
flash('tracker 已经存在.')
else:
flash('tracker 不存在,已插入.')
new_tracker = Trackers(name=trackers_name)
db.session.add(new_tracker)
db.session.commit()
return redirect(url_for('trackers_index'))
def contains_keywords(s, keywords):
return any(keyword in s for keyword in keywords)
@app.route('/trackers_delete/<int:trackers_name>', methods=['POST'], endpoint='delete_announcement_specific')
def delete_announcement_specific(trackers_name):
data = Trackers.query.filter_by(name=trackers_name).first()
if data:
db.session.delete(data)
db.session.commit()
flash(f'【{data}】已删除')
return redirect(url_for('trackers_index'))
else:
return 'Tracker not found'
@app.route('/trackers_delete', methods=['POST'], endpoint='delete_announcement')
def delete_announcement():
trackers_name = request.form.get('trackers_name') or request.view_args.get('trackers_name')
if not trackers_name:
return '什么都没输入,你删除啥呢!!!'
data = Trackers.query.filter_by(name=trackers_name).first()
if data:
db.session.delete(data)
db.session.commit()
flash(f'{data}已删除')
return redirect(url_for('trackers_index'))
else:
return f'数据库中不存在该数据!!!{trackers_name}'
@app.route('/trackers_update/<int:trackers_name>', methods=['POST'], endpoint='update_announcement')
def announcement_update(trackers_name):
data = Trackers.query.filter_by(name=trackers_name).first()
if data:
data.name = request.form.get('name')
data.is_usable = request.form.get('description')
db.session.commit()
flash('Data has been updated successfully.', 'success')
else:
flash('Data not found.', 'error')
return redirect(url_for('trackers_index'))
@app.route('/trackers_search', methods=['GET', 'POST'])
def announcement_search():
if request.method == 'POST':
trackers_name = request.form.get('trackers_name')
trackers = Trackers.query.filter(Trackers.name.like(f'%{trackers_name}%')).all()
return render_template('trackers_index.html', trackers=trackers)
return render_template('trackers_index.html')
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
if username == 'admin' and password == 'admin':
session['logged_in'] = True
session['username'] = username
flash("登陆成功")
return redirect(url_for('trackers_index'))
else:
return '无效的用户名或密码'
return redirect(url_for('trackers_index'))
@app.route('/logout', methods=['GET', 'POST'])
def logout():
session.pop('logged_in', None)
session.pop('username', None)
return redirect(url_for('trackers_index'))
if __name__ == '__main__':
# 创建数据库
create_tables()
app.run(debug=True, host='0.0.0.0', port=5000)
-
版权声明:
作者:kukey-admin
链接:https://kuyour.top/?p=395
来源:KuKey
文章版权归作者所有,未经允许请勿转载。
THE END
1
二维码
打赏
海报
flask-bt之tracker录入
templates\trackers_index.html
-
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="vi……
共有 0 条评论