Simple Rcon Connection With Python

Python's included with many Linux releases so it may be already installed on your server.. I believe the code should work for any game using Rcon.
Example #1
Sending messages...
import socket
if __name__ == "__main__":
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.connect(("12.321.12.321", 29670))
sock.send("xFFxFFxFFxFFrcon %s %s %s" % ("P4s5wrd","say"," ^5Message."))
sock.close()
Producing "CONSOLE: Message." in the game's chat area using Rcon's "say" (^5 is the color). Use "svsay" to print in the server message area.
Example #2
Sending a command, getting the output...
[....]
sock.send("xFFxFFxFFxFFrcon P4s5wrd status")
print sock.recv(65565)
sock.close()
Which prints the output of Rcon's 'status' command.
In both examples, be sure to replace the IP, port and password.
I'm using it to send automated timed admin messages to the game (a different post), but it could be used in any number of ways.
I initially worked with PHP to connect and send/receive different types of Rcon commands but that got tricky and fairly complicated in comparison to the Python route. Although controlling back-end Python scripts with front-end PHP would be fairly easy, and may be a future project.
Comments
Be the first to comment.


Leave a comment?