gameserver.py
import config
import os
from subprocess import Popen, PIPE, STDOUT
class GameserverHandler:
def __init__(self):
print("==== Gameserver ====")
self.__gameServerInstance__ = self.restart_gameserver()
print("\tGameserver started")
def restart_gameserver(self):
if os.path.exists(config.bot_config[0]['GameServer']['OutPutFile']):
os.remove(config.bot_config[0]['GameServer']['OutPutFile'])
f = open(config.bot_config[0]['GameServer']['OutPutFile'], "wb")
return Popen(config.bot_config[0]['GameServer']['Path'], stdin=PIPE, stdout=f, stderr=STDOUT)
def send_command(self, command):
try:
self.__gameServerInstance__.stdin.write(str.encode(command))
self.__gameServerInstance__.stdin.flush()
except BrokenPipeError:
pass
except OSError as e:
exit()
main.py
import config
import gameserver
gameServer = gameserver.GameserverHandler()
a = input()
gameServer.send_command('quit\n')
大家好,我刚写了第一个python脚本。这个脚本只是在我的计算机上启动一个游戏服务器,在一个文件中编写stdout和stderr,并给我一个向服务器发送命令的选项。
但是,当我使用send_command()时,游戏玩家没有得到stdin.write是有问题的。我读到我必须在它后面放一个冲水器()。但这无济于事。有趣的是,当我将代码更改为:
def send_command(self, command):
try:
self.__gameServerInstance__.stdin.write(str.encode(command))
self.__gameServerInstance__.stdin.flush()
self.__gameServerInstance__.stdout.flush()
self.__gameServerInstance__.stderr.flush()
except BrokenPipeError:
pass
except OSError as e:
exit()
我知道这个错误
Traceback (most recent call last): File "D:\Projekte\python\PycharmProjects\ServerLauncher\main.py", line 7, in <module>
gameServer.send_command('quit') File "D:\Projekte\python\PycharmProjects\ServerLauncher\gameserver.py", line 22, in send_command
self.__gameServerInstance__.stdout.flush() AttributeError: 'NoneType' object has no attribute 'flush'
我认为这是因为我将stdout,stderr设置为一个文件,但是为什么它比。
很抱歉有个愚蠢的问题,我刚开始编写python程序
Serge Ballesta接二连三的答复
更改代码: gameserver.py
def restart_gameserver(self):
if os.path.exists(config.bot_config[0]['GameServer']['OutPutFile']):
os.remove(config.bot_config[0]['GameServer']['OutPutFile'])
f = open(config.bot_config[0]['GameServer']['OutPutFile'], "wb")
return Popen(config.bot_config[0]['GameServer']['Path'], stdin=PIPE, stdout=f, stderr=STDOUT)
main.py
import config
import gameserver
import discord
gameServer = gameserver.GameserverHandler()
a = input()
print("SENDING")
gameServer.send_command('quit\n')
print("FINISH")
a = input()
更改:
subprocess.STDOUT
添加一条新行
但是,是的,不知道为什么,但过程还是没有得到退出。当我将所有内容放入main.py并删除类时,如下所示。
if os.path.exists(config.bot_config[0]['GameServer']['OutPutFile']):
os.remove(config.bot_config[0]['GameServer']['OutPutFile'])
f = open(config.bot_config[0]['GameServer']['OutPutFile'], "wb")
p = Popen(config.bot_config[0]['GameServer']['Path'], stdin=PIPE, stdout=f, stderr=STDOUT)
a = input()
p.stdin.write(b'quit')
它起作用了,我不知道为什么,会不会是stdin没有冲吗?
快速回答:Serge Ballesta
发布于 2021-12-20 15:20:41
代码中有许多不一致之处:
subprocess.STDOUT
:从子进程导入Popen,管道,STDOUT .返回Popen(config.bot_config‘’Path‘,stdin=PIPE,stdout=f,stdout=f
F=open(config.bot_config‘’OutPutFile‘,"wb")
quit
。大多数CLI程序期望命令以换行符结束。您应该在命令中添加一个\n
:self.__gameServerInstance__.stdin.write(str.encode(command) + 'b\n')
或
gameServer.send_command('quit\n')
在这些修复之后,我可以成功地启动一个cmd.exe
子进程(在exit\n
上),让它在exit\n
命令之后终止,并在输出文件中获得预期的数据。
https://stackoverflow.com/questions/70422840
复制相似问题