first commit
This commit is contained in:
46
app/services/RedisClient.py
Normal file
46
app/services/RedisClient.py
Normal file
@ -0,0 +1,46 @@
|
||||
"""
|
||||
@Time : 2022/10/9 11:53
|
||||
@Auth : 东
|
||||
@File :RedisClient.py
|
||||
@IDE :PyCharm
|
||||
@Motto:ABC(Always Be Coding)
|
||||
@Desc:
|
||||
|
||||
"""
|
||||
# coding:utf-8
|
||||
|
||||
import time
|
||||
import redis
|
||||
def redisClient():
|
||||
rc = redis.StrictRedis(host="localhost", port="6379", db=0, password="sdust2020")
|
||||
ps = rc.pubsub()
|
||||
ps.subscribe("liao") # 订阅消息
|
||||
a = 0
|
||||
|
||||
for item in ps.listen(): # 监听状态:有消息发布了就拿过来
|
||||
print(item)
|
||||
data = item['data']
|
||||
if type(data) == bytes:
|
||||
data = item['data'].decode()
|
||||
print(data)
|
||||
if data == '300030 -1':
|
||||
ps.unsubscribe("liao")
|
||||
|
||||
print(1)
|
||||
|
||||
|
||||
class Task(object):
|
||||
|
||||
def __init__(self, redis_conn, channel):
|
||||
self.rcon = redis_conn
|
||||
self.ps = self.rcon.pubsub()
|
||||
self.key = 'task:pubsub:%s' % channel
|
||||
self.ps.subscribe(self.key)
|
||||
|
||||
def listen_task(self):
|
||||
for i in self.ps.listen():
|
||||
if i['type'] == 'message':
|
||||
print("Task get ", i["data"])
|
||||
|
||||
def del_listen(self):
|
||||
self.ps.unsubscribe(self.key)
|
21
app/services/RedisService.py
Normal file
21
app/services/RedisService.py
Normal file
@ -0,0 +1,21 @@
|
||||
"""
|
||||
@Time : 2022/10/9 11:53
|
||||
@Auth : 东
|
||||
@File :RedisService.py
|
||||
@IDE :PyCharm
|
||||
@Motto:ABC(Always Be Coding)
|
||||
@Desc:
|
||||
|
||||
"""
|
||||
# coding:utf-8
|
||||
import json
|
||||
import time
|
||||
import redis
|
||||
|
||||
number_list = ['300033', '300032', '300031', '300030']
|
||||
signal = ['1', '-1', '1', '-1']
|
||||
|
||||
# rc = redis.StrictRedis(host='127.0.0.1', port='6379', db=3, password='sdust2020')
|
||||
# for i in range(len(number_list)):
|
||||
# value_new = {"ceshi": "测试"}
|
||||
# rc.publish("liao", json.dumps(value_new)) # 发布消息到liao
|
54
app/services/RpcClient.py
Normal file
54
app/services/RpcClient.py
Normal file
@ -0,0 +1,54 @@
|
||||
"""
|
||||
@Time : 2022/9/30 17:09
|
||||
@Auth : 东
|
||||
@File :RpcClient.py
|
||||
@IDE :PyCharm
|
||||
@Motto:ABC(Always Be Coding)
|
||||
@Desc:RPC客户端
|
||||
|
||||
"""
|
||||
|
||||
import json
|
||||
import socket
|
||||
import time
|
||||
|
||||
|
||||
class TCPClient(object):
|
||||
def __init__(self):
|
||||
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
|
||||
def connect(self, host, port):
|
||||
"""链接Server端"""
|
||||
self.sock.connect((host, port))
|
||||
|
||||
def send(self, data):
|
||||
"""将数据发送到Server端"""
|
||||
self.sock.send(data)
|
||||
|
||||
def recv(self, length):
|
||||
"""接受Server端回传的数据"""
|
||||
return self.sock.recv(length)
|
||||
|
||||
|
||||
class RPCStub(object):
|
||||
|
||||
def __getattr__(self, function):
|
||||
def _func(*args, **kwargs):
|
||||
d = {'method_name': function, 'method_args': args, 'method_kwargs': kwargs}
|
||||
self.send(json.dumps(d).encode('utf-8')) # 发送数据
|
||||
data = self.recv(1024) # 接收方法执行后返回的结果
|
||||
return data.decode('utf-8')
|
||||
|
||||
setattr(self, function, _func)
|
||||
return _func
|
||||
|
||||
|
||||
class RPCClient(TCPClient, RPCStub):
|
||||
pass
|
||||
|
||||
|
||||
# c = RPCClient()
|
||||
# c.connect('127.0.0.1', 5003)
|
||||
# print(c.add(1, 2, 3))
|
||||
# print(c.setData({"sss": "ssss", "list": [5, 2, 3, 4]}))
|
||||
|
55
app/services/RpcClient2.py
Normal file
55
app/services/RpcClient2.py
Normal file
@ -0,0 +1,55 @@
|
||||
"""
|
||||
@Time : 2022/9/30 17:09
|
||||
@Auth : 东
|
||||
@File :RpcClient.py
|
||||
@IDE :PyCharm
|
||||
@Motto:ABC(Always Be Coding)
|
||||
@Desc:RPC客户端
|
||||
|
||||
"""
|
||||
|
||||
import json
|
||||
import socket
|
||||
import time
|
||||
|
||||
|
||||
class TCPClient(object):
|
||||
def __init__(self):
|
||||
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
|
||||
def connect(self, host, port):
|
||||
"""链接Server端"""
|
||||
self.sock.connect((host, port))
|
||||
|
||||
def send(self, data):
|
||||
"""将数据发送到Server端"""
|
||||
self.sock.send(data)
|
||||
|
||||
def recv(self, length):
|
||||
"""接受Server端回传的数据"""
|
||||
return self.sock.recv(length)
|
||||
|
||||
|
||||
class RPCStub(object):
|
||||
|
||||
def __getattr__(self, function):
|
||||
def _func(*args, **kwargs):
|
||||
d = {'method_name': function, 'method_args': args, 'method_kwargs': kwargs}
|
||||
self.send(json.dumps(d).encode('utf-8')) # 发送数据
|
||||
data = self.recv(1024) # 接收方法执行后返回的结果
|
||||
return data.decode('utf-8')
|
||||
|
||||
setattr(self, function, _func)
|
||||
return _func
|
||||
|
||||
|
||||
class RPCClient(TCPClient, RPCStub):
|
||||
pass
|
||||
|
||||
|
||||
# c = RPCClient()
|
||||
# c.connect('127.0.0.1', 5003)
|
||||
# print(c.start('1'))
|
||||
# print(c.add(1, 2, 3))
|
||||
# print(c.setData({"sss": "ssss", "list": [1, 2, 3, 4]}))
|
||||
|
154
app/services/RpcService.py
Normal file
154
app/services/RpcService.py
Normal file
@ -0,0 +1,154 @@
|
||||
"""
|
||||
@Time : 2022/9/30 11:28
|
||||
@Auth : 东
|
||||
@File :RpcService.py
|
||||
@IDE :PyCharm
|
||||
@Motto:ABC(Always Be Coding)
|
||||
@Desc:RPC服务端
|
||||
|
||||
"""
|
||||
import asyncio
|
||||
import json
|
||||
import socket
|
||||
from functools import wraps
|
||||
|
||||
from app.schemas.TrainResult import ProcessValueList, Report
|
||||
from app.utils.RedisMQTool import Task
|
||||
from app.utils.StandardizedOutput import output_wrapped
|
||||
from app.utils.redis_config import redis_client
|
||||
|
||||
funcs = {}
|
||||
|
||||
|
||||
def register_function(func):
|
||||
"""
|
||||
server端方法注册,client端只能调用注册的方法
|
||||
"""
|
||||
name = func.__name__
|
||||
funcs[name] = func
|
||||
|
||||
|
||||
def mq_send(func, *args, **kwargs):
|
||||
data = func(*args, **kwargs)
|
||||
print(data)
|
||||
|
||||
|
||||
|
||||
class TCPServer(object):
|
||||
|
||||
def __init__(self):
|
||||
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
self.client_socket = None
|
||||
|
||||
def bind_listen(self, port=4999):
|
||||
self.sock.bind(('0.0.0.0', port))
|
||||
self.sock.listen(5)
|
||||
|
||||
def accept_receive_close(self):
|
||||
"""
|
||||
接收client的消息
|
||||
"""
|
||||
if self.client_socket is None:
|
||||
(self.client_socket, address) = self.sock.accept()
|
||||
if self.client_socket:
|
||||
msg = self.client_socket.recv(1024)
|
||||
data = self.on_msg(msg)
|
||||
self.client_socket.send(data)
|
||||
|
||||
def on_msg(self, msg):
|
||||
pass
|
||||
|
||||
|
||||
class RPCStub(object):
|
||||
def __init__(self):
|
||||
self.data = None
|
||||
|
||||
def call_method(self, data):
|
||||
"""
|
||||
解析函数,调用对应的方法便将该方法的执行结果返回
|
||||
"""
|
||||
if len(data) == 0:
|
||||
return json.dumps("something wrong").encode('utf-8')
|
||||
self.data = json.loads(data.decode('utf-8'))
|
||||
method_name = self.data['method_name']
|
||||
method_args = self.data['method_args']
|
||||
method_kwargs = self.data['method_kwargs']
|
||||
res = funcs[method_name](*method_args, **method_kwargs)
|
||||
return json.dumps(res).encode('utf-8')
|
||||
|
||||
|
||||
class RPCServer(TCPServer, RPCStub):
|
||||
def __init__(self):
|
||||
TCPServer.__init__(self)
|
||||
RPCStub.__init__(self)
|
||||
|
||||
def loop(self, port):
|
||||
"""
|
||||
循环监听 4999端口
|
||||
"""
|
||||
self.bind_listen(port)
|
||||
while True:
|
||||
try:
|
||||
self.accept_receive_close()
|
||||
except Exception:
|
||||
self.client_socket.close()
|
||||
self.client_socket = None
|
||||
print(Exception)
|
||||
continue
|
||||
|
||||
def on_msg(self, data):
|
||||
return self.call_method(data)
|
||||
|
||||
|
||||
def redisMQSend():
|
||||
def wrapTheFunction(func):
|
||||
@wraps(func)
|
||||
def wrapped_function(*args, **kwargs):
|
||||
data = func(*args, **kwargs)
|
||||
print(data)
|
||||
Task(redis_conn=redis_client.get_redis(), channel="ceshi").publish_task(data=output_wrapped(0, 'success', data))
|
||||
|
||||
return wrapped_function
|
||||
|
||||
return wrapTheFunction
|
||||
|
||||
|
||||
@register_function
|
||||
def add(a, b, c=10):
|
||||
sum = a + b + c
|
||||
print(sum)
|
||||
return sum
|
||||
|
||||
|
||||
@register_function
|
||||
def start(param: str):
|
||||
"""
|
||||
例子
|
||||
"""
|
||||
print(param)
|
||||
process_value_list = ProcessValueList(name='1', value=[])
|
||||
report = Report(rate_of_progess=0, process_value=[process_value_list])
|
||||
|
||||
@mq_send
|
||||
def process(v: int):
|
||||
print(v)
|
||||
report.rate_of_progess = ((v + 1) / 10) * 100
|
||||
report.process_value[0].value.append(v)
|
||||
|
||||
for i in range(10):
|
||||
process(i)
|
||||
print(report.dict())
|
||||
return report.dict()
|
||||
|
||||
|
||||
@register_function
|
||||
def setData(data):
|
||||
print(data)
|
||||
return data
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
# 开启redis连接
|
||||
redis_client.init_redis_connect()
|
||||
s = RPCServer()
|
||||
s.loop(5003) # 传入要监听的端口
|
134
app/services/TokenAuthService.py
Normal file
134
app/services/TokenAuthService.py
Normal file
@ -0,0 +1,134 @@
|
||||
import datetime
|
||||
from functools import wraps
|
||||
|
||||
from flask import g, jsonify, request
|
||||
|
||||
import jwt
|
||||
from app.configs.default import SECRET_KEY
|
||||
from flask_httpauth import HTTPTokenAuth, HTTPAuth
|
||||
|
||||
# 生成token,有效时间为 60*60 秒
|
||||
from app.exts import db
|
||||
|
||||
|
||||
def generate_auth_token(user_name, expiration=3600):
|
||||
reset_token = jwt.encode(
|
||||
{
|
||||
"user_name": user_name,
|
||||
"exp": datetime.datetime.now(tz=datetime.timezone.utc) + datetime.timedelta(seconds=expiration)
|
||||
},
|
||||
SECRET_KEY,
|
||||
algorithm="HS256"
|
||||
)
|
||||
return reset_token
|
||||
|
||||
|
||||
# 重写 HTTPTokenAuth
|
||||
class HTTPTokenAuthReReturn(HTTPTokenAuth):
|
||||
def __init__(self, scheme=None, realm=None, header=None):
|
||||
super().__init__(scheme, realm, header)
|
||||
|
||||
# 重写default_auth_error, 或者也可以重新定义一个函数
|
||||
# def default_auth_error(status):
|
||||
# return jsonify(data={}, message="token 错误!", code=status), status
|
||||
|
||||
def default_auth_error(status, message):
|
||||
return jsonify(data={}, message=message, code=status), status
|
||||
|
||||
# 如果重新定义函数的话,这里就传入新定义的函数名
|
||||
super().error_handler(default_auth_error)
|
||||
# 重写 login_required
|
||||
|
||||
def login_required(self, f=None, role=None, optional=None):
|
||||
if f is not None and \
|
||||
(role is not None or optional is not None): # pragma: no cover
|
||||
raise ValueError(
|
||||
'role and optional are the only supported arguments')
|
||||
|
||||
def login_required_internal(f):
|
||||
@wraps(f)
|
||||
def decorated(*args, **kwargs):
|
||||
auth = self.get_auth()
|
||||
|
||||
if request.method != 'OPTIONS':
|
||||
password = self.get_auth_password(auth)
|
||||
|
||||
status = None # 添加状态信息
|
||||
message = None
|
||||
user = self.authenticate(auth, password)
|
||||
# 这里判断verify_token的返回值是否是这里的一员
|
||||
if user in (False, None, 'BadSignature', 'SignatureExpired'):
|
||||
status = 401
|
||||
if user == 'BadSignature':
|
||||
message = "Bad Signature"
|
||||
elif user == 'SignatureExpired':
|
||||
message = "Signature Expired"
|
||||
elif not self.authorize(role, user, auth):
|
||||
status = 403
|
||||
message = "Forbidden"
|
||||
if not optional and status:
|
||||
# Clear TCP receive buffer of any pending data
|
||||
request.data
|
||||
try:
|
||||
# 因为之前重写了default_auth_error所以多传入一个message
|
||||
return self.auth_error_callback(status, message)
|
||||
except TypeError:
|
||||
return self.auth_error_callback()
|
||||
|
||||
g.flask_httpauth_user = user if user is not True \
|
||||
else auth.username if auth else None
|
||||
return f(*args, **kwargs)
|
||||
|
||||
return decorated
|
||||
|
||||
if f:
|
||||
return login_required_internal(f)
|
||||
return login_required_internal
|
||||
|
||||
|
||||
auth = HTTPTokenAuthReReturn(scheme="Bearer")
|
||||
|
||||
|
||||
# 获取用户权限
|
||||
@auth.get_user_roles
|
||||
def get_user_roles(user):
|
||||
the_role = get_role_token(user["token"])
|
||||
return the_role.role_key
|
||||
|
||||
|
||||
# 验证token
|
||||
@auth.verify_token
|
||||
def verify_token(token):
|
||||
try:
|
||||
print(token)
|
||||
data = jwt.decode(token, SECRET_KEY, algorithms=["HS256"])
|
||||
print(data)
|
||||
except jwt.ExpiredSignatureError:
|
||||
print("token过期")
|
||||
# 这里不用False,而是用自定义字符串
|
||||
return "SignatureExpired"
|
||||
except jwt.PyJWTError:
|
||||
print("token错误")
|
||||
# 这里不用False,而是用自定义字符串
|
||||
return "BadSignature"
|
||||
return True
|
||||
|
||||
|
||||
# 解析token不加密部分
|
||||
def token_parse(token):
|
||||
the_token = str.replace(str(token), 'Bearer ', '')
|
||||
decoded = jwt.decode(the_token, options={"verify_signature": False})
|
||||
return decoded
|
||||
|
||||
|
||||
# 根据 username 获取角色
|
||||
def get_role_username(username: str):
|
||||
return []
|
||||
|
||||
|
||||
# 根据 username 获取角色
|
||||
def get_role_token(token: str):
|
||||
user_info = token_parse(token)
|
||||
username = user_info["user_name"]
|
||||
the_role = get_role_username(username)
|
||||
return the_role
|
5
app/services/__init__.py
Normal file
5
app/services/__init__.py
Normal file
@ -0,0 +1,5 @@
|
||||
from app.core.common_utils import import_subs
|
||||
|
||||
__all__ = import_subs(locals())
|
||||
|
||||
|
Reference in New Issue
Block a user