Creating an RCON Manager (FastAPI Learning Edition #01)

Posted by

Creating an RCON manager is a useful project that can help you manage your game servers more effectively. In this tutorial, we will focus on using FastAPI to create a simple RCON manager. FastAPI is a modern web framework for building APIs with Python and it is known for its speed and ease of use.

To start, make sure you have Python installed on your system. You can check if Python is installed by running the following command in your terminal:

python --version

If you don’t have Python installed, you can download it from the official Python website and follow the installation instructions.

Next, we need to install FastAPI and uvicorn, which is a lightning-fast ASGI server. You can install them using pip, which is the Python package installer. Run the following commands in your terminal to install FastAPI and uvicorn:

pip install fastapi
pip install uvicorn

Now that we have all the necessary tools installed, let’s start by creating a new Python file for our RCON manager. Open your favorite code editor and create a new file called rcon_manager.py.

First, we need to import the necessary modules from FastAPI to get started. Add the following imports to your rcon_manager.py file:

from fastapi import FastAPI
from pydantic import BaseModel

Next, let’s create an instance of the FastAPI class. This will be our main application instance that will handle all the routing and requests. Add the following line to your rcon_manager.py file:

app = FastAPI()

Now, let’s define a model for our RCON requests. Create a new class called RCONRequest that inherits from the BaseModel class. This will define the structure of the data that will be sent in the requests. Add the following code to your rcon_manager.py file:

class RCONRequest(BaseModel):
    server_ip: str
    rcon_password: str
    command: str

Now, let’s define a route that will handle POST requests to execute RCON commands. Add the following code to your rcon_manager.py file:

@app.post("/rcon")
async def execute_rcon_command(rcon_request: RCONRequest):
    # Add your RCON command execution logic here
    return {"message": "RCON command executed successfully"}

In the code above, we defined a route at /rcon that will accept POST requests with JSON data conforming to the RCONRequest model. The execute_rcon_command function will execute the RCON command using the provided server IP, RCON password, and command.

Finally, let’s run our FastAPI application using uvicorn. In your terminal, run the following command:

uvicorn rcon_manager:app --reload

This will start the uvicorn server and load our FastAPI application from the rcon_manager.py file. You should see output indicating that the server is running.

That’s it! You have now created a simple RCON manager using FastAPI. You can test your RCON manager by sending POST requests to the /rcon endpoint with the required data. Feel free to customize and expand upon this project to suit your needs. Happy coding!