python - 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]
可以正常工作一段时间但我收到错误并且连接已关闭我做错了什么?
解决方案
关键在于堆栈跟踪中的最终错误消息:ConnectionAbortedError: [WinError 10053]
. 您可以在此处找到该问题的答案:ConnectionAbortedError: [WinError 10053] 已建立的连接已被主机中的软件中止
在以后的帖子中,请直接在帖子中包含源代码而不是图像,并且请以 ``` 格式输入错误输出,以便它是等宽的。
推荐阅读
- linux - unix 命令在日志中搜索 5XX 状态 HTTP 请求的计数
- weblate - 在 Weblate 中编辑源字符串的最佳方法
- sql - SELECT COUNT(*) WHERE DATE_PART(...) 在 PostgreSQL/TimescaleDB 中很慢
- jenkins - 当 Jenkins 作业 DSL 插件尝试删除一些未引用的作业时,是什么导致堆栈溢出错误?
- xamarin - 两个搜索栏 Xamarin Forms
- java - 如何使用递归在java中创建一系列子字符串
- linux - 如何使 Linux Docker 映像文件系统不区分大小写?
- c++ - 以下代码中的函数模板有什么问题?
- android - 从firebase检索数据到textview返回null如何解决?
- google-apps-script - 从 html 输入更新/编辑谷歌表格行值