[英]shell command stdout event in Python
I'm trying to watch wifi network changes from Python.我正在尝试从 Python 观察 wifi 网络变化。 So there is iwevent
which is a part of wireless_tools on Linux to watch events.所以有 iwevent 是iwevent
上的wireless_tools的一部分来观看事件。 Every time I'm switching wifi, iwevent
prints out following output每次我切换 wifi 时, iwevent
打印出 output
03:33:33.082474 wlan0 New Access Point/Cell address:Not-Associated
03:33:33.168659 wlan0 Scan request completed
03:33:33.234036 wlan0 Association Request IEs:00...
03:33:33.234109 wlan0 Association Response IEs:01...
03:33:33.234171 wlan0 New Access Point/Cell address:F...
So is there a way I can do this in an event driven way where Python watching stdout
in a thread and when it gets new stdout
it's doing to trigger a method?那么有没有一种方法可以以事件驱动的方式做到这一点,其中stdout
在线程中观察标准输出,当它获得新的stdout
时,它正在触发一个方法?
Wich wireless tool are you using, can you tell?你用的是哪个无线工具,你能告诉我吗?
Or, please, check if you could setting up a file to write log, instead to use sys.stdout
.或者,请检查您是否可以设置一个文件来写入日志,而不是使用sys.stdout
。 I think you can configure it by params (even using *args or **kwargs )我认为您可以通过参数配置它(即使使用*args或**kwargs )
OR If ou have access to the code (of the your wireless-tools), you can easily do it by:或者,如果您可以访问(您的无线工具的)代码,您可以通过以下方式轻松完成:
sys.stdout = open("/path/MyFileStdout.log", "w") sys.stdout = open("/path/MyFileStdout.log", "w")
And every print in your program will be written to the file, and you can monitoring the file easily, using python-watchdog
library, a straightforward Python API library to monitor file system events, like these examples:并且您程序中的每个打印都将写入文件,您可以使用python-watchdog
库轻松监控文件,这是一个简单的 Python API 库来监控文件系统事件,例如这些示例:
I hope this can help you, or, please, give us more information about your environment.我希望这可以帮助您,或者,请向我们提供有关您的环境的更多信息。
Best wishes!最好的祝愿!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.