Ошибка ConnectionResetError: [WinError 10054] websockets.exceptions.InvalidMessage websockets python - PullRequest
0 голосов
/ 04 мая 2020

Я получаю следующую ошибку после третьего запуска функции scrap_data. Я много искал его и пробовал несколько решений, таких как: - длительное время ожидания (до 30 с).
- установить интервал проверки связи на None - попробовать, кроме подключения через веб-сокет

, но ни одно из они работали.

мой код такой:

import requests
import json
import asyncio
import websockets
import urllib
import random
import time
import datetime
import pandas as pd
from threading import Thread

connectionData = [{"name":"..."}]
async def websocketConnect():
    def startReceiving(arg):
        r = ''
        while r == '':
            try:
                r = requests.get(".../realTimeServer/start", params = wsParams)
                break
            except:
                time.sleep(3)
                continue

    r = ''
    while r == '':
        try:
            r = requests.get(".../realTimeServer/negotiate", params = {
            "clientProtocol": "2.1",
            "connectionData": json.dumps(connectionData),
            })
            break
        except:
            time.sleep(3)
            continue

    response = r.json()


    wsParams = {
        "transport": "webSockets",
        "clientProtocol": "2.1",
        "connectionToken": response["ConnectionToken"],
        "connectionData": json.dumps(connectionData),
        "tid": random.randint(0,9)
    }
    websocketUri = f"wss:.../realTimeServer/connect?{urllib.parse.urlencode(wsParams)}"
    result = []
    async with websockets.connect(websocketUri, ping_interval=None) as websocket:
        thread = Thread(target = startReceiving, args = (0, ))
        thread.start()
        for i in range(0,10):
            try:
                data = await websocket.recv()
                jsonData = json.loads(data)
                #pars data
                if ("M" in jsonData and len(jsonData["M"]) > 0 and "A" in jsonData["M"][0] and len(jsonData["M"][0]["A"]) > 0):
                    items = jsonData["M"][0]["A"][0]
                    if type(items) == list and len(items) > 0: 
                        result = items
                        #crawled_item = items
                        break
            except:
                websocket = await websockets.connect(websocketUri, ping_interval=None)
        thread.join()
        global data_dic
        data_dic.append([datetime.datetime.now(), json.dumps(result, indent=4, sort_keys=True)])

    print(len(data_dic))
    return data_dic

def scrap_data():
    asyncio.ensure_future(websocketConnect())
    return data_dic

start_time = time.time()
interval = 1
for i in range(1,10):
    time.sleep(start_time + i*interval - time.time())
    data_dic = scrap_data()
    print(len(data_dic))

Ошибка: (Я получал это за каждый запуск scrap_data после 3-го запуска l oop)

Исключение задачи не было получено

future: <Task finished coro=<websocketConnect() done, defined at <ipython-input-3-c22626cfb25c>:27> exception=InvalidMessage('did not receive a valid HTTP response')>
Traceback (most recent call last):
  File "...\websockets\client.py", line 101, in read_http_response
    status_code, reason, headers = await read_response(self.reader)
  File "C:\Users\stock-1\Anaconda3\lib\site-packages\websockets\http.py", line 139, in read_response
    status_line = await read_line(stream)
  File "...\websockets\http.py", line 213, in read_line
    line = await stream.readline()
  File "...\asyncio\streams.py", line 496, in readline
    line = await self.readuntil(sep)
  File "C:\Users\stock-1\Anaconda3\lib\asyncio\streams.py", line 588, in readuntil
    await self._wait_for_data('readuntil')
  File "...\asyncio\streams.py", line 473, in _wait_for_data
    await self._waiter
  File "...\asyncio\selector_events.py", line 814, in _read_ready__data_received
    data = self._sock.recv(self.max_size)
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<ipython-input-3-c22626cfb25c>", line 29, in websocketConnect
    async with websockets.connect(websocketUri, ping_interval=None) as websocket:
  File "...\websockets\client.py", line 517, in __aenter__
    return await self
  File "...\websockets\client.py", line 547, in __await_impl__
    extra_headers=protocol.extra_headers,
  File "...\websockets\client.py", line 290, in handshake
    status_code, response_headers = await self.read_http_response()
  File "...\websockets\client.py", line 103, in read_http_response
    raise InvalidMessage("did not receive a valid HTTP response") from exc
websockets.exceptions.InvalidMessage: did not receive a valid HTTP response
...