如何验证Quart网络套接字?

问题描述 投票:0回答:1

摘自Quart的创建者的中篇文章:https://medium.com/@pgjones/websockets-in-quart-f2067788d1ee

认证

授权时控制接受的能力最有用请求,因为它允许检查请求标头,或者接受升级或返回401。一个简单的例子是,

def auth_required(func):
    @wraps(func)
    async def wrapper(*args, **kwargs):
        auth = websocket.authorization      
        if (
            auth is not None and
            auth.username == current.app.config['USERNAME'] and
            compare_digest(
                auth.password, current.app.config['PASSWORD'],
            )
        ):
            return await func(*args, **kwargs)
        else:
            abort(401)
    return wrapper

@app.websocket('/ws')
@auth_required
async def ws():
    ...

但是客户端javascript如何导致websocket.authorization属性被填充?

[var ws = new WebSocket('ws://' + 'user' + ':' + 'pass' + '@' + document.domain + ':' + location.port + '/ws');在最近几年中似乎已被弃用,并且不支持Websocket的HTTP请求中的自定义标头。

python python-3.x websocket python-asyncio quart
1个回答
0
投票
嗨,您能分享您的代码吗?这真的很有用...

谢谢

© www.soinside.com 2019 - 2024. All rights reserved.