首页 > 解决方案 > python3.9 websocket错误(不和谐网关)

问题描述

import websocket #pip install websocket-client
import json
import threading
import time

token_file = open("token.txt", "r")
token_read = token_file.read()
token_list = token_read.split()
token_file.close()
print(token_list)

def send_json_request(ws, request):
    ws.send(json.dumps(request))

def recieve_json_response(ws):
    response = ws.recv()
    if response:
        return json.loads(response)

def heartbeat(interval, ws):
    print('Heartbeat begin')
    while True:
        time.sleep(interval)
        heartbeatJSON = {
            "op": 1,
            "d": "null"
        }
        send_json_request(ws, heartbeatJSON)
        print("Heartbeat sent")

for index , v in enumerate(token_list):
    ws = websocket.WebSocket()
    ws.connect('wss://gateway.discord.gg/?v=6&encording=json')
    event = recieve_json_response(ws)

    heartbeat_interval = event['d']['heartbeat_interval'] / 1000
    threading._start_new_thread(heartbeat, (heartbeat_interval, ws))


    pis=1
    payload = {
        'op': 2,
        "d": {
            "token": v,
            "properties": {
                "$os": "windows",
                "$browser": "chrome",
                "$device": 'pc'
            }
        }
    }
    try:
        send_json_request(ws, payload)
    except:
        index += 1
        print(index, "error")

异常在由以下启动的线程中被忽略:<function heartbeat at 0x00000236C458D3A0> Traceback(最近一次调用最后一次):文件“C:\Users\pc\Desktop\test\websoket.py”,第 38 行,在 heartbeat send_json_request(ws, heartbeatJSON)文件“C:\Users\pc\Desktop\test\websoket.py”,第 16 行,在 send_json_request ws.send(json.dumps(request)) 文件“C:\Users\pc\AppData\Local\Programs\Python \Python39\lib\site-packages\websocket_core.py”,第 282 行,在发送返回 self.send_frame(frame) 文件“C:\Users\pc\AppData\Local\Programs\Python\Python39\lib\site-packages \websocket_core.py”,第 310 行,在 send_frame l = self._send(data) 文件“C:\Users\pc\AppData\Local\Programs\Python\Python39\lib\site-packages\websocket_core.py”,行514、在_send返回send(self.sock,data)文件"C:\Users\pc\AppData\Local\Programs\Python\Python39\lib\site-packages\websocket_socket.py”,第 175 行,在发送返回 _send() 文件“C:\Users\pc\AppData\Local\Programs\ Python\Python39\lib\site-packages\websocket_socket.py”,第 152 行,在 _send 返回 sock.send(data) 文件“C:\Users\pc\AppData\Local\Programs\Python\Python39\lib\ssl. py",第 1173 行,在发送中返回 self._sslobj.write(data) ConnectionAbortedError: [WinError 10053]

可以正常工作一段时间但我收到错误并且连接已关闭我做错了什么?

标签: pythonwebsocketdiscord

解决方案


关键在于堆栈跟踪中的最终错误消息:ConnectionAbortedError: [WinError 10053]. 您可以在此处找到该问题的答案:ConnectionAbortedError: [WinError 10053] 已建立的连接已被主机中的软件中止

在以后的帖子中,请直接在帖子中包含源代码而不是图像,并且请以 ``` 格式输入错误输出,以便它是等宽的。


推荐阅读