Created
October 28, 2012 13:14
-
-
Save mafice/3968557 to your computer and use it in GitHub Desktop.
Minecraftサーバのあれ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import os | |
import sys | |
import subprocess | |
import threading | |
import time | |
import re | |
cmd = "java -Xmx1024M -Xms1024M -jar minecraft_server.jar nogui" | |
def sendCmd (cmd, args): | |
str = cmd + " " + " ".join(args) + "\n" | |
serverPros.stdin.write(bytes(str)) | |
serverPros.stdin.flush() | |
def parseLog (log): | |
#login | |
if -1 != log.find("logged in with entity id"): | |
username = re.search("(.*?)\[[a-zA-Z0-9\/\.\:]*\]", log.split(" ")[3]).group(1) | |
sendCmd("tell", (username, "Greetings, " % (username,))) | |
class InputHandler (threading.Thread): | |
def run(self): | |
while True: | |
str = input("> ") | |
print("--- sending a command: %s" % str.split(" ")[0]) | |
if str == "quit": | |
print("stopping the minecraft server, please wait...") | |
sendCmd("stop", (,)) | |
time.sleep(5) | |
serverPros.terminate() | |
print("Good byte ;-)") | |
os._exit(0) | |
sendCmd(str) | |
class ServerMsgHandler (threading.Thread): | |
def run(self): | |
with open("server.log") as log: | |
while True: | |
for line in log.readlines(): | |
parseLog(line) | |
sys.stdout.flush() | |
time.sleep(1) | |
oldLoadAvg = 0 | |
class ReportHandler(threading.Thread): | |
def run (self): | |
global oldLoadAvg | |
while True: | |
time.sleep(5 * 60) | |
loadAvg = round(os.getloadavg()[1], 2) | |
status = "Load Avg. is %s (%f+)" % (loadAvg, round(loadAvg - oldLoadAvg, 2)) | |
oldLoadAvg = loadAvg | |
sendCmd("tell", ("mafice", status)) | |
print("Welcome to Minecraft Server!") | |
os.remove("server.log") | |
open("server.log", "w").close() | |
serverPros = subprocess.Popen(cmd.split(" "), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
inputHandler = InputHandler() | |
serverMsgHandler = ServerMsgHandler() | |
reportHandler = ReportHandler() | |
reportHandler.start() | |
serverMsgHandler.start() | |
inputHandler.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment