Django配置websocket请求接口

1.settings.py

INSTALLED_APPS = [
    '...',
    'channels',
    '...',
]
ASGI_APPLICATION = 'server.routing.application'
CHANNEL_LAYERS = {
    'default': {
        'BACKEND': 'channels_redis.core.RedisChannelLayer',
        'CONFIG': {
            "hosts": [(os.getenv('REDIS_SERVER_HOST', '127.0.0.1'), int(os.getenv('REDIS_SERVER_PORT', '6379')))],
        },
    },
}

2.routing.py(settin.py同级)

# -*- coding: utf-8 -*-

from channels.routing import ProtocolTypeRouter, URLRouter

from device.consumers import QueryAuthMiddleware  # websocket中间件
import device.routing  # 路由


application = ProtocolTypeRouter({
    'websocket': QueryAuthMiddleware(
        URLRouter(
            device.routing.websocket_urlpatterns
        )
    )
})
class QueryAuthMiddleware:
    """
    WebSocket 认证中间件
    """
    def __init__(self, inner):
        # Store the ASGI application we were passed
        self.inner = inner

    def __call__(self, scope):
        query = parse.parse_qs(scope["query_string"].decode())
        token = query.get('token', [None])[0]
        device_name = None
        try:
            login_info = json.loads(base64.b64decode(token).decode())
            device_name = login_info['name']
            logger.info('Device {} connect from {} port {}'.format(device_name, scope['client'][0], scope['client'][1]))

            # 验证时间
            gen_time = datetime.strptime(login_info['time'], '%Y%m%d%H%M%S')
            now_time = datetime.now()
            if abs(gen_time - now_time).total_seconds() > 5 * 60:
                raise ValueError('Device {} time validate fail: token time: {}, server time: {}'.format(
                    device_name, gen_time.strftime('%Y-%m-%d %H:%M:%S'), now_time.strftime('%Y-%m-%d %H:%M:%S')))

            # 验证密码
            device = DeviceInfoModel.objects.get(name=device_name)  # 获取到设备
            correct = device.check_password(login_info['password'])  # 检查密码
            if not correct:
                raise ValueError('Device {} password validate fail'.format(device_name))

        except DeviceInfoModel.DoesNotExist:
            logger.info('Device {} can not find in DB'.format(device_name))
            device = None

        except Exception as e:
            logger.error('Connect error {}'.format(e))
            device = None

        finally:
            # Middleware 中必须手动关闭数据库连接
            # http://channels.readthedocs.io/en/latest/topics/authentication.html#custom-authentication
            close_old_connections()

        return self.inner(dict(scope, user=device))

参考:https://channels.readthedocs.io/en/latest/

 

转载于:https://www.cnblogs.com/52-qq/p/11451435.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值