我正在尝试在python中编写一个非常简单的电子邮件脚本.这基本上是一个穷人的笨蛋.在工作中,我们从服务器发送大量数据,并且直接从服务器发送它会容易得多.
我坚持的部分是处理消息.我希望用户能够执行以下操作:
$ cat message.txt | emailer.py fandingo@example.com
$ tail -n 2000 /var/log/messages | emailer.py fandingo@example.com
Run Code Online (Sandbox Code Playgroud)
这两个都很容易.我可以sys.stdin.read()
获取我的数据.
我遇到的问题是我还想支持输入带有以下用法的消息的提示:
emailer.py --attach-file /var/log/messages fandingo@example.com
Enter Your message. Use ^D when finished.
>> Steve,
>> See the attached system log. See all those NFS errors around 2300 UTC today.
>>
>> ^D
Run Code Online (Sandbox Code Playgroud)
我遇到的麻烦是,如果我尝试sys.stdin.read()
,并且没有数据,那么我的程序会阻塞,直到stdin获取数据,但我无法打印我的提示.我可以采取安全的方法而raw_input("Enter Your message. Use ^D when finished.")
不是使用stdin.read()
,但随后我总是打印提示.
有没有办法看看用户是否在不使用会阻塞的方法的情况下将文本传输到python中?
zee*_*kay 18
您可以sys.stdin.isatty
用来检查脚本是否以交互方式运行.例:
if sys.stdin.isatty():
message = raw_input('Enter your message ')
else:
message = sys.stdin.read()
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6253 次 |
最近记录: |