• Python hacks

    From chibiko@911:1423/0 to All on Sunday, October 13, 2024 16:31:53
    This is a solution to the problem about acting on a log file as it is being written to, without much of a hassle. There's a way to do it purely in Python, but why do it when we can RE use it??
    It goes like this:

    --- cut here ---

    import re
    import subprocess

    log = '/home/user/log'

    f = subprocess.Popen(['tail', '-F', log], stdout=subprocess.PIPE)

    while True:
    line = f.stdout.readline()
    match = re.search(b'debug', line) # search for pattern
    if match:
    # do something!

    --- cut here ---

    Subprocess is used to open a tail process and send data to stdout, then there's an infinite loop that keeps reading a line and searches for a pattern to trigger an action.
    The main reason this should be used is to answer to logs in a fast way. It processes all lines passed through it. So, overall, pretty easy to use.

    --- Mystic BBS v1.12 A47 2021/12/25 (Windows/32)
    * Origin: d i s t o r t i o n // d1st.org (911:1423/0)
  • From paulie420@911:1503/0 to chibiko on Sunday, October 13, 2024 19:00:50
    This is a solution to the problem about acting on a log file as it is being written to, without much of a hassle. There's a way to do it
    purely in Python, but why do it when we can RE use it??

    The main reason this should be used is to answer to logs in a fast way.
    It processes all lines passed through it. So, overall, pretty easy to
    use.

    Simple pimple - thanks for sharing your log watcher!



    |07p|15AULIE|1142|07o
    |08.........

    --- Mystic BBS v1.12 A49 2024/05/29 (Linux/64)
    * Origin: 2o fOr beeRS bbS>>20ForBeers.com:1337 (911:1503/0)