Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import subprocess
- import shlex
- import time
- import threading
- interval = 2 # in seconds
- command = 'ping 8.8.8.8'
- def print_(s):
- """2 and 3 compatible printing for most output. e.g don't use it.
- Use print without newline of your python version
- """
- sys.stdout.write(s.decode("utf-8"))
- sys.stdout.flush()
- latest = None
- def run_command(command):
- global latest
- process = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE)
- while True:
- latest = process.stdout.readline()
- if latest == '' and process.poll() is not None:
- break
- rc = process.poll()
- return rc
- def print_output(interval=0):
- time.sleep(0.1)
- while True:
- time.sleep(interval)
- print_(latest)
- # instead of print send to the database
- runner = threading.Thread(target=run_command, args=[command,])
- output = threading.Thread(target=print_output, args=[interval,])
- runner.start()
- output.start()
Add Comment
Please, Sign In to add comment