简体   繁体   中英

Websockets closing connection immediately after handshake

I am trying to open a secure WebSocket and when connection is open, I want to send JSON request and to wait for response. Need it for automation BackEnd testing. But, I try this code and I get

websockets.exceptions.ConnectionClosedError: code = 3000 (registered), no reason

My colleague tells me that it seems that I open the connection, do the handshake and then immediately close it! I would like to remain open if it is possible! This is the code that returns me "error" 3000

import asyncio
import websockets
import ssl
import logging
import time

logger = logging.getLogger('websockets')
logger.setLevel(logging.INFO)
logger.addHandler(logging.StreamHandler())


serv = 'wss://"adress":"PORT"'
print("1")
ssl_context = ssl.SSLContext(protocol=ssl.PROTOCOL_TLSv1_2)
print("2")
json_s = {"msgType":1,"tag":12345,"username":"abc","password":"defghijk"}
print("3")
async def get_msg(uri):
    async with websockets.connect(uri) as ws:
        await ws.send(json_s)
        print("4")
        time.sleep(3)
        print('data sent')
        time.sleep(3)
        resp = await ws.recv()
        print("5")
        time.sleep(3)


    return resp
print("6")

loop = asyncio.get_event_loop().run_until_complete(get_msg(serv))
print("7")
loop.create_task(get_msg(serv))
print("8")
loop.run_forever()
print("9")

I am getting

    1
    2
    3
    6
    4
    data sent
    Traceback (most recent call last):
      File "C:/Users/Korisnik/PycharmProjects/TACHYON/Test/Websockets doc test.py", line 34, in <module>
        loop = asyncio.get_event_loop().run_until_complete(get_msg(serv))
      File "C:\Users\Korisnik\AppData\Local\Programs\Python\Python38-32\lib\asyncio\base_events.py", line 608, in run_until_complete
        return future.result()
      File "C:/Users/Korisnik/PycharmProjects/TACHYON/Test/Websockets doc test.py", line 26, in get_msg
        resp = await ws.recv()
      File "C:\Users\Korisnik\PycharmProjects\TACHYON1\venv\lib\site-packages\websockets\protocol.py", line 509, in recv
        await self.ensure_open()
      File "C:\Users\Korisnik\PycharmProjects\TACHYON1\venv\lib\site-packages\websockets\protocol.py", line 812, in ensure_open
        raise self.connection_closed_exc()
    **websockets.exceptions.ConnectionClosedError: code = 3000 (registered), no reason**

Process finished with exit code 1

And I am stuck here...

Maybe someone will find this Answer helping Our server was set-up to get connection "Only" From web browser, so I must put in websocket settings User-Agent and Origin headers! It looks like this

async with websockets.connect(uri, max_size=None, max_queue=2 ** 5, read_limit=2 ** 20, write_limit=2 ** 20, extra_headers={'User-Agent': 'Safari/537.36', 'Origin': '192.168.1.1'}) as websocketESU: When I put those extra headers everything works just fine It takes me 2 moths to find this solution which is absolutely simple! Thanks everyone for answers!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM