简体   繁体   English

shell 命令标准输出事件在 Python

[英]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 库来监控文件系统事件,例如这些示例:

  1. https://blog.magrathealabs.com/filesystem-events-monitoring-with-python-9f5329b651c3 https://blog.magrathealabs.com/filesystem-events-monitoring-with-python-9f5329b651c3
  2. http://timgolden.me.uk/python/win32_how_do_i/watch_directory_for_changes.html http://timgolden.me.uk/python/win32_how_do_i/watch_directory_for_changes.html

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.

相关问题 在后台运行一个shell命令,并使用python将stdout传递给一个日志文件 - run a shell command in the background and pipe stdout to a logfile with python 我如何生成一个shell子命令,其标准输出绑定到python中父级的标准输出 - How do I spawn a shell sub-command, with its stdout bound to the parent's stdout in python 在python,shell和stdout中使用ffmepg - using ffmepg in python, shell and stdout Python执行shell并打印stdout - Python to execute shell and prints the stdout Python - 运行 shell 命令,获取 stdout 和 stderr 作为变量,但对用户隐藏? - Python - Run shell command, get stdout and stderr as a variable, but hide from user? 在 python 中,我如何仅打印由 shell 命令产生的标准输出的第一行 - In python how can i print only the first line of stdout which results from a shell command Python代码将stdin和stdout重定向到Shell脚本 - Python code to redirect stdin and stdout to a shell script 在没有 shell 转义序列的 Python 中打印标准输出 - Print stdout in Python without shell escape sequences Shell 命令 - 延迟标准输出日志,因为在命令执行时路径不存在 - Shell command - delay stdout log as path does not exist at command execution python-实时捕获子进程的stderr及其shell stdout - python - capturing stderr of subprocesses with their shell stdout alive
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM