This repository has been archived on 2024-07-30. You can view files and clone it, but cannot push or open issues or pull requests.
python-aternos/examples/websocket_args_example.py

48 lines
919 B
Python
Raw Normal View History

2022-05-18 19:35:46 +04:00
import asyncio
import logging
2022-05-18 19:35:46 +04:00
from getpass import getpass
from typing import Tuple, Dict, Any
from python_aternos import Client, Streams
# Request credentials
2022-05-18 19:35:46 +04:00
user = input('Username: ')
pswd = getpass('Password: ')
# Debug logging
logs = input('Show detailed logs? (y/n) ').strip().lower() == 'y'
if logs:
logging.basicConfig(level=logging.DEBUG)
# Authentication
2022-05-18 19:35:46 +04:00
aternos = Client.from_credentials(user, pswd)
server = aternos.list_servers()[0]
socket = server.wss()
2022-05-18 19:35:46 +04:00
2022-06-23 15:13:56 +04:00
# Handler for console messages
@socket.wssreceiver(Streams.console, ('Server 1',))
async def console(
msg: Dict[Any, Any],
args: Tuple[str]) -> None:
2022-05-18 19:35:46 +04:00
print(args[0], 'received', msg)
2022-06-23 15:13:56 +04:00
# Main function
async def main() -> None:
server.start()
2022-05-18 19:35:46 +04:00
await socket.connect()
await asyncio.create_task(loop())
2022-06-23 15:13:56 +04:00
# Keepalive
async def loop() -> None:
2022-05-18 19:35:46 +04:00
while True:
await asyncio.Future()
2022-05-18 19:35:46 +04:00
asyncio.run(main())