无法在python3中发出websockets请求

时间:2018-08-28 10:12:38

标签: python-3.x websocket

我正在尝试在python3中复制以下js代码

var ws = new f("wss://10.20.3.33:11443/ext/remoteinput");
ws.binaryType = 'arraybuffer';
ws.onopen = function() {
    const buffer = new ArrayBuffer(3);
    const uint8 = new Uint8Array(buffer);
    uint8.set([1, 204, 1]);

    ws.send(uint8)
    console.log("Message is sent...");
};

我尝试使用: https://github.com/websocket-client/websocket-client https://github.com/crossbario/autobahn-python 还有什么...

这个挂在websockets.connect

import asyncio, websockets, ssl
ssl_context = ssl.SSLContext()
# ssl_context = ssl.SSLContext(protocol=ssl.CERT_NONE)
ssl_context.verify_mode = ssl.CERT_NONE

async def hello():
    async with websockets.connect(
            'wss://10.20.3.33:11443/ext/remoteinput', ssl=ssl_context) as websocket:
        name = b'\x01\xCC\x01'
        print("2")
        await websocket.send(name)
        print(f"> {name}")

        greeting = await websocket.recv()
        print(f"< {greeting}")

asyncio.get_event_loop().run_until_complete(hello())

与此相同:

import websocket, ssl

ws = websocket.create_connection("wss://10.20.3.33:11443/ext/remoteinput",
sslopt={"cert_reqs": ssl.CERT_NONE})
ws.send(b'\x01\xCC')
ws.close()


import asyncio
from autobahn.asyncio.websocket import WebSocketClientProtocol, \
    WebSocketClientFactory

最后一次超时,但是等待更长的时间无法解决任何问题

  

使用abort ==断开与对等方tcp:10.20.3.33:11443的连接,是真的:WebSocket打开握手超时(对等未及时完成打开握手)

class MyClientProtocol(WebSocketClientProtocol):

    def onConnect(self, response):
        print("Server connected: {0}".format(response.peer))

    def onOpen(self):
        self.sendMessage(b"\x00\x01\x03\x04", isBinary=True)


if __name__ == '__main__':
    factory = WebSocketClientFactory(u"wss://10.20.3.33:11443/ext/remoteinput")
    factory.protocol = MyClientProtocol

    loop = asyncio.get_event_loop()
    coro = loop.create_connection(factory, '10.20.3.33', 11443)
    loop.run_until_complete(coro)
    loop.run_forever()
    loop.close()

是否可以通过某种方式扩展内置python套接字以使用wss url?或者也许有一种方法可以使上述示例之一起作用

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('10.20.3.33', 11443))
msg = s.send(b'\x01\xCC\x01')
s.close()

0 个答案:

没有答案
相关问题