简介:rcon库可以适用Source(起源引擎)、BattlEye(BE交互系统)、MINECRAFT(我的世界java1.9pre4后加入Source RCON协议)的交互
一、安装rcon库
pip3 install rcon
二、使用代码
例如,我们要连接的服务器ip是127.0.0.1:25565,设置的RCON密码是114514,要输入指令status:
HOST = '127.0.0.1'PORT = 25565PASSWD = '114514'COMMAND = 'status'
1、Source(适用游戏:我的世界、求生之路2、CS、GMOD等等)
from rcon.source import Clientwith Client(HOST, PORT, PASSWD) as client: response = client.run(COMMAND)print(response)
2、BattlEye(适用游戏:绝地求生、彩虹六号、Unturned、方舟生存进化、H1Z1等)
from rcon.battleye import Clientwith Client(HOST, PORT, PASSWD) as client: response = client.run(COMMAND)print(response)
三、进阶
1、使用协程程序:使用rcon()即可
from rcon.source import rconresponse = await rcon(HOST, PORT, PASSWD)print(response)
2、处理服务器信息
from rcon.battleye import Clientfrom rcon.battleye.proto import ServerMessagedef my_message_handler(server_message: ServerMessage) -> None: """Print server messages.""" print('Server message:', server_message)with Client(HOST, PORT, PASSWD message_handler=my_message_handler) as client: response = client.run(COMMAND)print('输出:', response)
参考:rcon · PyPI