Archived
feat: upload files
This commit is contained in:
10 files changed
+46
-21
No files matched your search
@@ -7,19 +7,16 @@
|
|||||||
|
|
||||||
|
|
||||||
import time
|
import time
|
||||||
|
from gevent import pywsgi
|
||||||
from flask import Flask
|
from flask import Flask
|
||||||
from flask import request
|
from flask import request
|
||||||
from flask import Response
|
from flask import Response
|
||||||
from flask import make_response
|
from flask import make_response
|
||||||
from flask import send_from_directory
|
from flask import send_from_directory
|
||||||
from bin.render_ import render_temp_
|
from bin.core.render_ import render_temp_
|
||||||
from bin.b64img import re_sort_number_image
|
from bin.core.b64img import re_sort_number_image
|
||||||
from bin.length import make_html
|
from bin.core.length import make_html
|
||||||
from wsgiref.simple_server import make_server
|
from db.db import (fetch_data, update_data)
|
||||||
from db.db import (
|
|
||||||
fetch_data,
|
|
||||||
update_data
|
|
||||||
)
|
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
@@ -39,13 +36,15 @@ def error_length(length: int) -> Response:
|
|||||||
return response
|
return response
|
||||||
|
|
||||||
|
|
||||||
def too_lang_to_count() -> Response:
|
def too_lang_to_count(name: str) -> Response:
|
||||||
"""
|
"""
|
||||||
数据过长重置数据
|
数据过长重置数据
|
||||||
|
:param name:
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
|
update_data(name, 0)
|
||||||
response = make_response({'code': 200,
|
response = make_response({'code': 200,
|
||||||
'message': f'当前长度超过了预设的值'})
|
'message': f'当前长度超过了最大可计数长度:10. 已将重置该名称的计数器'})
|
||||||
response.headers['Content-Type'] = 'application/json; charset=utf-8'
|
response.headers['Content-Type'] = 'application/json; charset=utf-8'
|
||||||
response.headers['cache-control'] = 'max-age=0, no-cache, no-store, must-revalidate'
|
response.headers['cache-control'] = 'max-age=0, no-cache, no-store, must-revalidate'
|
||||||
response.headers['date'] = time.ctime()
|
response.headers['date'] = time.ctime()
|
||||||
@@ -66,7 +65,7 @@ def arg_not_be_full() -> Response:
|
|||||||
return response
|
return response
|
||||||
|
|
||||||
|
|
||||||
def build_page(name: str, length: int) -> Response | str | bool:
|
def build_page(name: str, length: int) -> list[Response] | list[bool | str] | list[bool]:
|
||||||
"""
|
"""
|
||||||
渲染最终的页面
|
渲染最终的页面
|
||||||
:param name:
|
:param name:
|
||||||
@@ -75,14 +74,14 @@ def build_page(name: str, length: int) -> Response | str | bool:
|
|||||||
"""
|
"""
|
||||||
count = fetch_data(name)
|
count = fetch_data(name)
|
||||||
if len(str(count)) > length:
|
if len(str(count)) > length:
|
||||||
return too_lang_to_count()
|
return [False, too_lang_to_count(name)]
|
||||||
if 7 <= length <= 10:
|
if 7 <= length <= 10:
|
||||||
make_html(length)
|
make_html(length)
|
||||||
zero_count = '0' * (length - len(str(count))) + str(count)
|
zero_count = '0' * (length - len(str(count))) + str(count)
|
||||||
sorted_image = re_sort_number_image(zero_count)
|
sorted_image = re_sort_number_image(zero_count)
|
||||||
return render_temp_(length, name, sorted_image)
|
return [True, render_temp_(length, name, sorted_image)]
|
||||||
else:
|
else:
|
||||||
return False
|
return [False, 'BadLength']
|
||||||
|
|
||||||
|
|
||||||
@app.route('/API', methods=['GET', 'POST']) # 允许 GET 和 POST 方法
|
@app.route('/API', methods=['GET', 'POST']) # 允许 GET 和 POST 方法
|
||||||
@@ -102,14 +101,16 @@ def api_page() -> Response | str:
|
|||||||
name = request.args['name']
|
name = request.args['name']
|
||||||
if name != '': # 数据为空返回参数不完整界面
|
if name != '': # 数据为空返回参数不完整界面
|
||||||
resp = build_page(name, length)
|
resp = build_page(name, length)
|
||||||
if resp:
|
if resp[0]:
|
||||||
response = make_response(resp)
|
response = make_response(resp[1])
|
||||||
response.headers['Content-Type'] = 'image/svg+xml; charset=utf-8'
|
response.headers['Content-Type'] = 'image/svg+xml; charset=utf-8'
|
||||||
response.headers['cache-control'] = 'max-age=0, no-cache, no-store, must-revalidate'
|
response.headers['cache-control'] = 'max-age=0, no-cache, no-store, must-revalidate'
|
||||||
response.headers['date'] = time.ctime()
|
response.headers['date'] = time.ctime()
|
||||||
return response
|
return response
|
||||||
else:
|
elif resp[1] == 'BadLength':
|
||||||
return error_length(length)
|
return error_length(length)
|
||||||
|
else:
|
||||||
|
return resp[1]
|
||||||
else:
|
else:
|
||||||
return arg_not_be_full()
|
return arg_not_be_full()
|
||||||
else:
|
else:
|
||||||
@@ -126,5 +127,10 @@ def index():
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
server = make_server('', 5000, app)
|
# app.run(threaded=True)
|
||||||
|
print('服务器已在http://127.0.0.1:5000 运行. \n除了请求次数和名称会被保存到本地数据库中其余任何数据都不会被保存')
|
||||||
|
try:
|
||||||
|
server = pywsgi.WSGIServer(('0.0.0.0', 5000), app)
|
||||||
server.serve_forever()
|
server.serve_forever()
|
||||||
|
except OSError:
|
||||||
|
print('5000 端口被占用')
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
output:
|
||||||
|
# 日志等级 DEBUG INFO WARNING ERROR CRITICAL
|
||||||
|
level: INFO
|
||||||
|
# 日志是否开启颜色 True False
|
||||||
|
color: True
|
||||||
|
|
||||||
|
servers:
|
||||||
|
# 地址 string
|
||||||
|
host: 0.0.0.0
|
||||||
|
# 端口 integer
|
||||||
|
port: 5000
|
||||||
|
|
||||||
@@ -2,5 +2,5 @@
|
|||||||
# -- coding:utf-8 --
|
# -- coding:utf-8 --
|
||||||
# @Author: markushammered@gmail.com
|
# @Author: markushammered@gmail.com
|
||||||
# @Development Tool: PyCharm
|
# @Development Tool: PyCharm
|
||||||
# @Create Time: 2022/2/3
|
# @Create Time: 2022/2/5
|
||||||
# @File Name: __init__.py.py
|
# @File Name: __init__.py.py
|
||||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
BIN
Binary file not shown.
@@ -1,2 +1,4 @@
|
|||||||
requests~=2.27.1
|
requests~=2.27.1
|
||||||
Flask~=2.0.2
|
Flask~=2.0.2
|
||||||
|
colorlog~=6.6.0
|
||||||
|
gevent~=21.12.0
|
||||||
Binary file not shown.
|
After Width: | Height: | Size: 4.5 MiB |
@@ -7,6 +7,11 @@
|
|||||||
.intro{
|
.intro{
|
||||||
text-align: center;
|
text-align: center;
|
||||||
}
|
}
|
||||||
|
body{
|
||||||
|
background-image: url("./static/background.jpg");
|
||||||
|
background-repeat: no-repeat;
|
||||||
|
background-size: cover;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
|||||||
Reference in New Issue
Block a user