【问题标题】:Getting output and sending input to a minecraft server with the subprocess module使用 subprocess 模块获取输出并将输入发送到 minecraft 服务器
【发布时间】:2020-03-27 18:53:39
【问题描述】:

我正在尝试获取我的世界服务器输出并能够发送输入。我有这个代码:

pipe = subprocess.Popen(f"java -Xms{RAM} -Xmx{RAM} -jar server.jar nogui", cwd=os.path.join(os.getcwd(), "server"), stdout=subprocess.PIPE, stdin=subprocess.PIPE, shell=True, text=True)

while True:
    pipe.stdin.write(input("Enter a command: "))
    print("1")
    print(pipe.stdout.readline(), end="")
    pipe.stdout.flush()
    print("3")

它似乎每次都会启动服务器的输入,但是一旦它完全加载并且我输入一个命令,它就会冻结。我怎样才能防止这种情况发生?

【问题讨论】:

  • 为什么你认为每行输入你会得到正好 1 行的输出?
  • @Joseph 我不知道。我想将最新的输出行绑定到一个变量,并使其与输入流无关,这样它们就可以相互独立地工作。

标签: python server subprocess minecraft


【解决方案1】:

你可以这样使用:

with open("start.bat", "w+") as terminal:
    terminal.write("say Hello World!")
    output = terminal.readlines()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-15
    • 1970-01-01
    • 2014-11-29
    • 2014-12-04
    相关资源
    最近更新 更多