H. *_*ons 2 python-3.x minecraft servermanager server
我对此进行了很多搜索,但尚未找到明确的解决方案。我发现的最接近的是:
import shutil
from os.path import join
import os
import time
import sys
minecraft_dir = ('server diectory')
world_dir = ('server world driectory')
def server_command(cmd):
os.system('screen -S -X stuff "{}\015"'.format(cmd))
on = "1"
while True:
command=input()
command=command.lower()
if on == "1":
if command==("start"):
os.chdir(minecraft_dir)
os.system('"C:\Program Files\Java\jre1.8.0_111\bin\java.exe" -Xms4G -Xmx4G -jar craftbukkit-1.10.2.jar nogui java')
print("Server started.")
on = "0"
else:
server_command(command)
Run Code Online (Sandbox Code Playgroud)
当我启动这个程序并输入“开始”时,CMD 会闪烁并立即关闭。相反,我希望 CMD 保持打开状态,并从中运行 Minecraft 服务器。我不确定为什么会发生这种情况或问题是什么,任何帮助将不胜感激。
ps我已经根据我的需要对此进行了编辑(例如删除不必要的备份脚本),但它之前不起作用。原始链接是: https: //github.com/tschuy/minecraft-server-control
os.system将简单地运行该命令,然后返回到您的 python 脚本,无法进一步与其通信。
另一方面,使用subprocess.Popen可以让您在进程运行时访问该进程,包括写入它,.stdin这就是将数据发送到服务器的方式:
def server_command(cmd):
process.stdin.write(cmd+"\n") #just write the command to the input stream
process = None
executable = '"C:\Program Files\Java\jre1.8.0_111\bin\java.exe" -Xms4G -Xmx4G -jar craftbukkit-1.10.2.jar nogui java'
while True:
command=input()
command=command.lower()
if process is not None:
if command==("start"):
os.chdir(minecraft_dir)
process = subprocess.Popen(executable, stdin=subprocess.PIPE)
print("Server started.")
else:
server_command(command)
Run Code Online (Sandbox Code Playgroud)
你也可以通过,stdout=subprocess.PIPE这样你也可以读取它的输出和stderr=subprocess.PIPE从它的错误流中读取(如果有)
process.stdin.write(cmd+"\n")您也可以使用print 函数的可选参数来代替file,因此:
print(cmd, file=process.stdin)
Run Code Online (Sandbox Code Playgroud)
将以与 print 通常相同的方式将数据写入 process.stdin 格式,例如以换行符结尾,除非传递end=以覆盖它等。
小智 6
上述两个答案在我尝试过的环境中都不起作用。
我认为最好的方法是使用 RCON,而不是将密钥发送到窗口。
RCON 是游戏用来运行命令的协议。
许多 python 库支持 Minecraft RCON,默认的 server.properties 文件有一个 RCON 选项。
我们将使用 python 模块:MCRON。
安装它。它适用于 Windows、Mac、Linux。
类型:
pip install mcrcon
Run Code Online (Sandbox Code Playgroud)
让我们配置您的服务器以允许 RCON。
在 server.properties 中,找到“enable-rcon”行并使其如下所示:
enable-rcon=true
Run Code Online (Sandbox Code Playgroud)
重新启动并停止您的服务器。找到“rcon.password”行并将其设置为您能记住的任何密码。
您可以将端口默认保留为 25575。
现在,打开终端并输入:
mcron localhost
Run Code Online (Sandbox Code Playgroud)
或者你的服务器ip。
系统将提示您输入您设置的密码。然后你可以运行命令并得到结果。
但我们使用 python,而不是 PYPI MCRON 脚本来完成此操作 - 所以这样做。
from mcrcon import MCRcon as r
with r('localhost', 'insertyourpasswordhere') as mcr:
resp = mcr.command('/list')
print(resp) #there are 0/20 players online: - This will be different for you.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16253 次 |
| 最近记录: |