【发布时间】:2019-06-03 17:34:19
【问题描述】:
我的 python 程序运行缓慢,因为任何请求都会重新连接套接字。我想进行一次连接并发送请求,而不需要重新连接
我在文件send_by_socket.py 中的函数,其他一些函数和类调用send_to_socket 用于发送日志消息。现在它可以工作了,但是很慢。原因 - 为任何消息建立新连接。我想要单个连接或轮询以使用它而无需重新连接。如何制作它,可能有很好的源代码示例?
import asyncio
import websockets
from logging import StreamHandler
import json
async def async_send(message):
async with websockets.connect('wss://****.com/chat') as web_socket:
await web_socket.send(message)
class WebSocketHandler(StreamHandler):
def __init__(self):
StreamHandler.__init__(self)
def emit(self, record):
msg = json.dumps({'log': {'message': record.message, 'date': record.asctime, 'level': record.levelname}})
try:
asyncio.get_event_loop().run_until_complete(async_send(msg))
except ConnectionRefusedError:
pass
def send_to_socket(msg_dict):
msg = json.dumps(msg_dict)
try:
asyncio.get_event_loop().run_until_complete(async_send(msg))
except ConnectionRefusedError:
pass
现在程序花费大约 1 - 1.2 秒来处理请求。我试试
con = websockets.connect('wss://****.com/chat')
con.send('some thing')
但有错误AttributeError: 'Connect' object has no attribute 'send'
【问题讨论】:
-
wss 协议适用于 https 你试过使用 ws 吗?它适用于 http ...我建议你使用 http 和 ws 让它工作,然后一旦它的工作转移到 https 和 wss
-
绝对不能使用 ws。需要 wss。
标签: python-3.x websocket