黑马程序员技术交流社区

标题: 【上海校区】Django配置websocket请求接口 [打印本页]

作者: 梁强    时间: 2019-10-31 14:18
标题: 【上海校区】Django配置websocket请求接口
1.settings.py
[Python] 纯文本查看 复制代码
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同级)
[Python] 纯文本查看 复制代码
# -*- 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
        )
    )
})

[Python] 纯文本查看 复制代码
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))

此时,启动方式也应该稍作调整(项目根目录下新建asgi.py文件)
[Python] 纯文本查看 复制代码
"""
ASGI entrypoint. Configures Django and then runs the application
defined in the ASGI_APPLICATION setting.
"""

import os
import django
from channels.routing import get_default_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "server.settings")
django.setup()
application = get_default_application()

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





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2