首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么python stdin.write()在stdin.flush()之后不能工作?

为什么python stdin.write()在stdin.flush()之后不能工作?
EN

Stack Overflow用户
提问于 2021-12-20 13:50:17
回答 1查看 237关注 0票数 0

gameserver.py

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
import config
import gameserver


gameServer = gameserver.GameserverHandler()
a = input()
gameServer.send_command('quit\n')

大家好,我刚写了第一个python脚本。这个脚本只是在我的计算机上启动一个游戏服务器,在一个文件中编写stdout和stderr,并给我一个向服务器发送命令的选项。

但是,当我使用send_command()时,游戏玩家没有得到stdin.write是有问题的。我读到我必须在它后面放一个冲水器()。但这无济于事。有趣的是,当我将代码更改为:

代码语言:javascript
运行
复制
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() 

我知道这个错误

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
import config
import gameserver
import discord

gameServer = gameserver.GameserverHandler()
a = input()
print("SENDING")
gameServer.send_command('quit\n')
print("FINISH")
a = input()

更改:

subprocess.STDOUT

  • change文件操作到“wb”

  • 向send_command

添加一条新行

但是,是的,不知道为什么,但过程还是没有得到退出。当我将所有内容放入main.py并删除类时,如下所示。

代码语言:javascript
运行
复制
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

EN

回答 1

Stack Overflow用户

发布于 2021-12-20 15:20:41

代码中有许多不一致之处:

  1. 您对stdout和stderr都使用相同的文件。这是错误的,并可能导致不正确的输出文件。您应该使用特殊值subprocess.STDOUT

从子进程导入Popen,管道,STDOUT .返回Popen(config.bot_config‘’Path‘,stdin=PIPE,stdout=f,stdout=f

  1. 您用字节IO定义子进程,但将输出文件打开为文本。您应该使用二进制模式:

F=open(config.bot_config‘’OutPutFile‘,"wb")

  1. 您发送命令quit。大多数CLI程序期望命令以换行符结束。您应该在命令中添加一个\n

self.__gameServerInstance__.stdin.write(str.encode(command) + 'b\n')

gameServer.send_command('quit\n')

在这些修复之后,我可以成功地启动一个cmd.exe子进程(在exit\n上),让它在exit\n命令之后终止,并在输出文件中获得预期的数据。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70422840

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档