asyncio Python3.6代码到asyncio Python3.4 cod

2024-09-27 19:32:17 发布

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

我有一个3.6异步代码:

async def send(command,userPath,token):
    async with websockets.connect('wss://127.0.0.1:7000',ssl=ssl.SSLContext(protocol=ssl.PROTOCOL_TLS)) as websocket:
        data = json.dumps({"api_command":"session","body":command,"headers": {'X-User-Path': userPath, 'X-User-Token': token}})
        await websocket.send(data)
        response = await websocket.recv()
        response = json.loads(response)
        if 'command' in response:
            if response['command'] == 'ACK_COMMAND' or response['command'] == 'ACK_INITIALIZATION':
                return (response['message'],200)
        else:
            return(response,400)

我把它转换成了3.4异步代码

^{pr2}$

虽然解释器运行转换,但当我调用函数时,会出现以下错误:

with websockets.connect('wss://127.0.0.1:7000',ssl=ssl.SSLContext(protocol=ssl.PROTOCOL_TLS)) as websocket:
AttributeError: __enter__

我觉得有更多的东西要转换,但我不知道是什么。如何使3.4代码工作?在

注意:我用3.6python运行3.4代码


Tags: 代码tokensendsslasyncwebsocketsresponseconnect
1条回答
网友
1楼 · 发布于 2024-09-27 19:32:17

async with websockets.connecthere中可以找到,您应该这样做:

websocket = yield from websockets.connect('ws://localhost:8765/')
try:
    # your stuff
finally:
    yield from websocket.close()

在你的情况下,应该是:

^{pr2}$

相关问题 更多 >

    热门问题