gpt4 book ai didi

python - 如何判断 Python 程序是否有任何可从标准输入读取的内容?

转载 作者:太空宇宙 更新时间:2023-11-03 13:52:22 24 4
gpt4 key购买 nike

我正在用 Python 编写一个聊天程序,它需要在接受来自 sys.stdin 的用户输入之前连接到服务器。如果无法建立连接,则程序退出。

从 shell 运行它,如果连接失败并且在尝试连接时发送了输入,则在程序退出后将输入回显到 shell:

jtink@gab-dec:~$ python chat.py
attempting to connect...
Hey there! # This is user input
How are you? # This is more user input
connection failed
jtink@gab-dec:~$ Hey there!
Hey there!: command not found
jtink@gab-dev:~$ How are you?
How are you?: command not found
jtink@gab-dev:~$

有没有办法判断 sys.stdin 上是否还有输入,以便我可以在聊天程序退出之前读取它?

我已阅读 this similar question ,但答案只描述了如何判断输入是否正在通过管道传输到 python 程序,而不是如何判断是否有输入。

最佳答案

我认为你需要检查这个关于 stdin 上的非阻塞 I/O 的问题 Non-blocking read on a subprocess.PIPE in python

关于python - 如何判断 Python 程序是否有任何可从标准输入读取的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4936594/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com