如何在 websocket python 中发送“标题" [英] How to send 'Headers' in websocket python

查看:24
本文介绍了如何在 websocket python 中发送“标题"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我该怎么做

这是我的代码

<预><代码>导入网络套接字异步定义测试():与 websockets.connect('ws://iqoption.com') 异步作为 websocket:响应 = 等待 websocket.recv()打印(响应)# 客户端异步代码

提示是,我可以发送此标头以在服务器中进行身份验证

标题

 'Host':'iqoption.com','用户代理': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:73.0) Gecko/20100101 Firefox/73.0','接受' : '*/*','接受编码':'gzip,放气','Sec-WebSocket-Version': '13','来源':'https://iqoption.com','Sec-WebSocket-Key': 'iExBWv1j6sC1uee2qD+QPQ==','连接':'保持活动,升级','升级':'websocket'}

我可以用这个其他代码来做,但消息仍然用 tls 加密

proxys = {"http": "http://127.0.0.1:8080", "https": "http://127.0.0.1:8080"}urllib3.disable_warnings()r = requests.get('https://iqoption.com/echo/websocket', stream=True,headers = Headers, proxies=proxies, verify=False)#r = requests.get('https://iqoption.com/echo/websocket', stream=True,headers = Headers)s = socket.fromfd(r.raw.fileno(), socket.AF_INET, socket.SOCK_STREAM)def receive_and_print():#for message in iter(lambda: s.recv(1024).decode("utf-8", errors="replace"), ''):对于 iter(lambda: s.recv(1024).decode( errors="replace"), '') 中的消息:打印(:",消息)打印("")进口螺纹background_thread = threading.Thread(target=receive_and_print)background_thread.daemon = 真background_thread.start()而 1:s.send(input("请输入您的信息:").encode())打印(已发送")打印("")

有什么建议吗??

解决方案

我认为您目前缺少对 WebSockets 的基本了解,正如您之前的实验所示.WebSockets 不是普通的套接字.WebSockets 是在 HTTP 握手之后创建的一些类似套接字的想法.您不能像在 requests 之后尝试的那样从连接中获取套接字,但您必须明确说明在 RFC 6455.

也就是说,使用像 websockets 这样的库要好得多.但是您仍然必须使用实际的 WebSocket 端点作为目标,而不仅仅是同一服务器上的某个任意 URL.这种情况下的端点不是ws://iqoption.comwss://iqoption.com/echo/websocket,即更长的路径和wss://而不是 ws://.

如果没有正确的 URL,您会收到错误消息,您似乎将其解释为身份验证问题.但是这里根本不涉及身份验证.一旦您使用了正确的端点,它就可以正常工作而无需发送特定的标头:

async def test():与 websockets.connect("wss://iqoption.com/echo/websocket") 异步作为 websocket:响应 = 等待 websocket.recv()打印(响应)

how can i make this

This is my code


import  websockets


async def test():
    async with websockets.connect('ws://iqoption.com') as websocket:
        response = await websocket.recv()
        print(response)
# Client async code

The cuestion is , can i send this headers to get Authenticated in the server

Headers

    'Host':'iqoption.com',
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:73.0) Gecko/20100101 Firefox/73.0',
    'Accept' : '*/*',
'Accept-Encoding': 'gzip, deflate',
'Sec-WebSocket-Version' : '13',
'Origin' : 'https://iqoption.com',
    'Sec-WebSocket-Key': 'iExBWv1j6sC1uee2qD+QPQ==',
'Connection' : 'keep-alive, Upgrade',
'Upgrade': 'websocket'}

I could do it with this other code but the messages were still encrypted with tls

proxies = {"http": "http://127.0.0.1:8080", "https": "http://127.0.0.1:8080"}
urllib3.disable_warnings()

r = requests.get('https://iqoption.com/echo/websocket', stream=True,headers = Headers, proxies=proxies, verify=False)
#r = requests.get('https://iqoption.com/echo/websocket', stream=True,headers = Headers)
s = socket.fromfd(r.raw.fileno(), socket.AF_INET, socket.SOCK_STREAM)


def receive_and_print():
    #for message in iter(lambda: s.recv(1024).decode("utf-8", errors="replace"), ''):
    for message in iter(lambda: s.recv(1024).decode( errors="replace"), ''):
        print(":", message)
        print("")
import threading
background_thread = threading.Thread(target=receive_and_print)
background_thread.daemon = True
background_thread.start()

while 1:
    s.send(input("Please enter your message: ").encode())
    print("Sent")
    print("")

Any advice??

解决方案

I think you are currently missing a basic understanding of WebSockets as is shown on your previous experiments. WebSockets are not plain sockets. WebSockets are some socket-like think created after a HTTP handshake. You cannot just take the socket from the connection as you've tried after requests but you have to explicitly speak the WebSocket application protocol, defined in RFC 6455.

That said, using a library like websockets is much better. But you still have to use the actual WebSocket endpoint as target and not just some arbitrary URL on the same server. The endpoint in this case is not ws://iqoption.com but wss://iqoption.com/echo/websocket, i.e. a longer path and wss:// instead of ws://.

Without the proper URL you get error messages you seem to interpret as authentication problems. But there is no authentication involved here at all. Once you use the proper endpoint it simply works without any need to send specific headers:

async def test():
    async with websockets.connect("wss://iqoption.com/echo/websocket") as websocket:
        response = await websocket.recv()
        print(response)

这篇关于如何在 websocket python 中发送“标题"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆