当我正要回答一些相关信息时,这个问题被删除了。我认为,尽管它的措辞方式让人不喜欢它,并且没有发布代码,但它是一个有用的问题。因此,我决定将其连同我的部分答案发布在这里。我当前的代码有问题,如果有人知道解决方案,我会很高兴听到它。另外,如果有人知道更清晰的解决方案(例如使用 Popen
对象的 communicate
方法),那也很好。
据我记得,问题的相关部分是这样的:
如何使用 Python 与 Minecraft 服务器通信?我设置了一个用户界面,但我不确定如何连接到服务器并向其发送命令。
最佳答案
这是我正在使用的代码,我稍微编辑了 rlms 的代码来解决 readline 问题:
import os, sys, time
import subprocess
server = subprocess.Popen('./start.sh',stdin=subprocess.PIPE,shell=True)
content = ''
previousContent = ''
while True:
#you can add a time.sleep() to reduce lag
f = open('logs/latest.log')
content = f.read()
if previousContent in content:
content.replace(previousContent,'')
if content != '':
print(content)
command = input('')
if command:
server.stdin.write(bytes(command + '\r\n', 'ascii'))
server.stdin.flush()
previousContent = f.read()
关于python - 使用Python与minecraft服务器通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20892875/