I desperately want to load a Factorio server via Python, with the support of STDin/out. Bestow upon this feeble-minded soul the incantations needed to achieve this feat without resorting to the dark arts of RCON. Any tips, tricks, or enchanted scripts you could share would not only save my sanity but might just make you a hero that saves me from my demise.
Here is a sample script of mine.
Code: Select all
import asyncio
import subprocess
async def run_factorio_server():
factorio_exe = "Factorio Server\\bin\\x64\\factorio.exe"
save_file = "Factorio Server\\saves\\JustMap.zip"
# Command to start the Factorio server with the specified save file
command = f'"{factorio_exe}" --start-server "{save_file}"'
process = await asyncio.create_subprocess_shell(
command,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
print("started...")
async def read_output():
while True:
line = await process.stdout.readline()
if line:
print("Server output:", line.decode().strip())
else:
break
async def send_commands():
await asyncio.sleep(10)
print("Sending /save command to Factorio server...")
process.stdin.write(b"/save\n")
await process.stdin.drain()
await asyncio.sleep(10)
print("Sending /quit command to stop the server...")
process.stdin.write(b"/quit\n")
await process.stdin.drain()
await asyncio.gather(
read_output(),
send_commands()
)
await process.wait()
print("stopped")
if __name__ == "__main__":
asyncio.run(run_factorio_server())