python3.9 websocket错误(不协调网关)

2024-06-06 10:36:25 发布

您现在位置:Python中文网/ 问答频道 /正文

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")

在以下线程中忽略异常:<;0x00000236C458D3A0处的功能检测信号>; 回溯(最近一次呼叫最后一次): 文件“C:\Users\pc\Desktop\test\websocket.py”,第38行,在heartbeat中 发送json请求(ws,heartbeatJSON) 文件“C:\Users\pc\Desktop\test\websoket.py”,第16行,在send\u json\u请求中 发送(json.dumps(请求)) 文件“C:\Users\pc\AppData\Local\Programs\Python39\lib\site packages\websocket\u core.py”,第282行,在send中 返回自发送帧(帧) 文件“C:\Users\pc\AppData\Local\Programs\Python39\lib\site packages\websocket\u core.py”,第310行,在send\u框架中 l=自发送(数据) 文件“C:\Users\pc\AppData\Local\Programs\Python\Python39\lib\site packages\websocket\u core.py”,第514行,在发送中 返回发送(self.sock,数据) 文件“C:\Users\pc\AppData\Local\Programs\Python39\lib\site packages\websocket\u socket.py”,第175行,在send中 返回_send() 文件“C:\Users\pc\AppData\Local\Programs\Python39\lib\site packages\websocket\u socket.py”,第152行,在发送 返回sock.send(数据) 文件“C:\Users\pc\AppData\Local\Programs\Python\Python39\lib\ssl.py”,第1173行,在send中 返回self.\u sslobj.write(数据) ConnectionAbortedError:[WinError 10053]

在一段时间内工作正常,但出现错误,连接已关闭 我做错了什么?


Tags: 文件pytokensendjsonwsliblocal
1条回答
网友
1楼 · 发布于 2024-06-06 10:36:25

该键位于堆栈跟踪中的最后一条错误消息中:ConnectionAbortedError: [WinError 10053]。你可以在这里找到这个问题的答案:ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine

在以后的文章中,请将源代码直接包含在文章中,而不是图像中,并请以“``格式输入错误输出,以便等距

相关问题 更多 >