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

52 lines
957 B
Python
Raw Normal View History

2022-05-18 19:35:46 +04:00
import asyncio
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: ')
# Instantiate Client
atclient = Client()
aternos = atclient.account
# Enable debug logging
logs = input('Show detailed logs? (y/n) ').strip().lower() == 'y'
if logs:
atclient.debug = True
# Authenticate
atclient.login(user, pswd)
2022-05-18 19:35:46 +04:00
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',)) # type: ignore
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())