Python websockets连接被拒绝
我试图执行这段使用python库websockets的代码,但我无法连接,因为服务器拒绝连接并终止连接 突然好像有什么东西挡住了我的请求,我不知道是什么。以下是我的websockets库代码:Python websockets连接被拒绝,python,websocket,async-await,Python,Websocket,Async Await,我试图执行这段使用python库websockets的代码,但我无法连接,因为服务器拒绝连接并终止连接 突然好像有什么东西挡住了我的请求,我不知道是什么。以下是我的websockets库代码: import requests import asyncio import json import traceback import websockets async def async_processing(): async def on_message(message): prin
import requests
import asyncio
import json
import traceback
import websockets
async def async_processing():
async def on_message(message):
print(message)
# here the code to process message
async def get_url():
url = 'https://rtds.retabet.es/realTimeDataHub/negotiate?negotiateVersion=1'
headers = {
"Host": "rtds.retabet.es",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:81.0) Gecko/20100101 Firefox/81.0",
"Accept": "*/*",
"Accept-Language": "es-ES,es;q=0.8,en-US;q=0.5,en;q=0.3",
"Accept-Encoding": "gzip, deflate, br",
"Referer": "https://apuestas.retabet.es/",
"X-Requested-With": "XMLHttpRequest",
"Content-Type": "text/plain;charset=UTF-8",
"Origin": "https://apuestas.retabet.es",
"Connection": "keep-alive",
"Content-Length": "0",
}
resp = requests.post(url, headers=headers,timeout=5)
resp = json.loads(resp.text)
connection_id = resp["connectionToken"].strip()
url = 'wss://rtds.retabet.es/realTimeDataHub?id={}'.format(connection_id)
return url
url = await get_url()
async with websockets.connect(url) as websocket:
print("opened")
await websocket.send('{"protocol":"json","version":1}\x1e')
while True:
try:
message = await websocket.recv()
await on_message(message)
except Exception as e:
print('ConnectionClosed')
print(traceback.format_exc())
raise
async def other_tasks():
while True:
print("Here I will do the task")
await asyncio.sleep(1)
asyncio.get_event_loop().run_until_complete(asyncio.wait([
other_tasks(),
async_processing()
], return_when=asyncio.FIRST_EXCEPTION))
或者,我也尝试使用websocket库。在这种情况下,我可以连接websocket,但我的循环中的并行任务在websocket连接后不会执行
打开了。下面是我的websocket库代码:
import requests
import websocket
import asyncio
import json
async def async_processing():
async def on_message(message):
print(message)
# here the code to process message
async def get_url():
url = 'https://rtds.retabet.es/realTimeDataHub/negotiate?negotiateVersion=1'
headers = {
"Host": "rtds.retabet.es",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:81.0) Gecko/20100101 Firefox/81.0",
"Accept": "*/*",
"Accept-Language": "es-ES,es;q=0.8,en-US;q=0.5,en;q=0.3",
"Accept-Encoding": "gzip, deflate, br",
"Referer": "https://apuestas.retabet.es/",
"X-Requested-With": "XMLHttpRequest",
"Content-Type": "text/plain;charset=UTF-8",
"Origin": "https://apuestas.retabet.es",
"Connection": "keep-alive",
"Content-Length": "0",
}
resp = requests.post(url, headers=headers,timeout=5)
resp = json.loads(resp.text)
connection_id = resp["connectionToken"].strip()
url = 'wss://rtds.retabet.es/realTimeDataHub?id={}'.format(connection_id)
return url
url = await get_url()
websocket.enableTrace(True)
ws = websocket.create_connection(url)
print("opened")
ws.send('{"protocol":"json","version":1}\x1e')
while True:
try:
message = ws.recv()
await on_message(message)
except Exception as e:
print(e)
print('ConnectionClosed')
ws.close()
break
async def other_tasks():
while True:
print("Here I will do the task")
await asyncio.sleep(1)
asyncio.get_event_loop().run_until_complete(asyncio.wait([
other_tasks(),
async_processing()
], return_when=asyncio.FIRST_EXCEPTION))
我的目标是连接websocket并并行运行一些东西。我以前使用WebSocket连接过一些WebSocket,但我遇到了任何问题,所以我需要知道为什么现在不可能。如果有人能让我了解一下这个话题,我将不胜感激。
另外,知道第二段中的错误也很好,因为我有点困惑
谢谢