小编Eug*_*nov的帖子

如何使用 node.js 获取屏幕分辨率

我需要使用 node.js 获取屏幕分辨率,但以下代码不起作用。

var w = screen.width;
var h = screen.height;
Run Code Online (Sandbox Code Playgroud)

这也行不通。

var w = window.screen.width;
var h = window.screen.height;
Run Code Online (Sandbox Code Playgroud)

有人知道如何使用 node.js 获得屏幕分辨率吗?谢谢你。

javascript client-server screen-resolution node.js

5
推荐指数
2
解决办法
2万
查看次数

Python asyncio:如何模拟__aiter __()方法?

我有一个代码,正在使用侦听WebSocket上的消息aiohttp

看起来像:

async for msg in ws:
    await self._ws_msg_handler.handle_message(ws, msg, _services)
Run Code Online (Sandbox Code Playgroud)

原始代码ws的实例在哪里aiohttp.web.WebSocketResponse()

在我的测试中,我模拟了WebSocketResponse()它及其__aiter__方法:

def coro_mock(**kwargs):
    return asyncio.coroutine(mock.Mock(**kwargs))


@pytest.mark.asyncio
@mock.patch('aiojsonrpc.request_handler.WebSocketMessageHandler')
async def test_rpc_websocket_handler(
    MockWebSocketMessageHandler,
    rpc_websocket_handler
):

    ws_response = 'aiojsonrpc.request_handler.WebSocketResponse'
    with mock.patch(ws_response) as MockWebSocketResponse:
        MockRequest = mock.MagicMock()
        req = MockRequest()

        ws_instance = MockWebSocketResponse.return_value
        ws_instance.prepare = coro_mock()
        ws_instance.__aiter__ = coro_mock(return_value=iter(range(5)))
        ws_instance.__anext__ = coro_mock()

        handle_msg_result = 'Message processed'
        MockWebSocketMessageHandler.handle_message.side_effect = Exception(
            handle_msg_result)
        msg_handler = MockWebSocketMessageHandler()

        with pytest.raises(Exception) as e:
            await request_handler.RpcWebsocketHandler(msg_handler)(req)
        assert str(e.value) …
Run Code Online (Sandbox Code Playgroud)

pytest python-3.x python-asyncio aiohttp python-unittest.mock

3
推荐指数
3
解决办法
2004
查看次数

我应该使用 HMAC 摘要的 Base64 还是只使用 HMAC 十六进制摘要?

传奇

我公开了一个 API,它要求客户端通过发送两个标头来签署请求:

Authorization: MyCompany access_key:<signature>
Unix-TimeStamp: <unix utc timestamp in seconds>
Run Code Online (Sandbox Code Playgroud)

要创建签名部分,客户端应使用我的 API 服务发布的密钥。

在 Python (Py3k) 中,它看起来像:

import base64
import hmac
from hashlib import sha256
from datetime import datetime

UTF8 = 'utf-8'
AUTH_HEADER_PREFIX = 'MyCompany'

def create_signature(access_key, secret_key, message):
    new_hmac = hmac.new(bytes(secret_key, UTF8), digestmod=sha256)
    new_hmac.update(bytes(message, UTF8))
    signature_base64 = base64.b64encode(new_hmac.digest())
    return '{prefix} {access_key}:{signature}'.format(
        prefix=AUTH_HEADER_PREFIX,
        access_key=access_key,
        signature=str(signature_base64, UTF8).strip()
    )


if __name__ == '__main__':
    message = str(datetime.utcnow().timestamp())
    signature = create_signature('my access key', 'my secret key',  message)
    print(
        'Request headers are',
        'Authorization: …
Run Code Online (Sandbox Code Playgroud)

python encode digital-signature hmac python-3.x

2
推荐指数
1
解决办法
5479
查看次数