首页 > 解决方案 > 如何使用 WebSocket 发送消息?

问题描述

我在 DevTools 中打开了 Firefox 网络选项卡,我想通过 WhatsApp 发送一条消息,它使用 WebSockets 进行通信。这是发送消息时的样子:

开发工具上的 Whatsapp

现在,我想通过 Python 发送一个 Web Socket 消息,但我找不到方法。我用谷歌搜索了它,但找不到关于如何实现这一目标的适当解决方案。我不确切知道要提供哪些参数来发送消息。

到目前为止,这是我的代码:

import requests

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0',
    'Accept': '*/*',
    'Accept-Language': 'en-US,en;q=0.5',
    'Sec-WebSocket-Version': '13',
    'Origin': 'https://web.whatsapp.com',
    'Sec-WebSocket-Extensions': 'permessage-deflate',
    'Sec-WebSocket-Key': 'yVoOx/6Sn1wyiv9R3OopPQ==',
    'Connection': 'keep-alive, Upgrade',
    'Pragma': 'no-cache',
    'Cache-Control': 'no-cache',
    'Upgrade': 'websocket',
    'TE': 'Trailers',
}

response = requests.get("http://wss://web.whatsapp.com/ws", headers=headers)

标签: python-3.xwebsocketpython-requestsrequestgoogle-chrome-devtools

解决方案


推荐阅读