1
J'ai ce 3.6 Code async:python asyncio 3.6 code à asyncio python 3.4 Code
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)
que je converti à ce code 3.4 async
@asyncio.coroutine
def send(command,userPath,token):
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}})
yield from websocket.send(data)
response = yield from 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)
Bien que l'interprète exécute la conversion, quand je appelez la fonction cette erreur se produit:
with websockets.connect('wss://127.0.0.1:7000',ssl=ssl.SSLContext(protocol=ssl.PROTOCOL_TLS)) as websocket:
AttributeError: __enter__
Je me sens comme il y a plus de choses à convertir, mais je ne sais pas quelle. Comment puis-je faire fonctionner le code 3.4?
Note: Je lance le 3.4 code avec un 3.6 python
'async with' ne peut pas être remplacé par plain' with'. Voir aussi https://stackoverflow.com/questions/37465816/async-with-in-python-3-4 – VPfB