# along with this program; if not, write to the Free Software Foundation, # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA # # Example: # tail -f /var/log/syslog | zstdio.py import sys import zmq import socket import fcntl, os from zocp import ZOCP if __name__ == '__main__': hostname = socket.gethostname() z = ZOCP("zstdio@{0}".format(hostname)) z.register_string("stdin", "", 're') z.start() # we need to set stdin to non blocking otherwise # a read could potentially block flags = fcntl.fcntl(sys.stdin, fcntl.F_GETFL) fcntl.fcntl(sys.stdin, fcntl.F_SETFL, flags | os.O_NONBLOCK) zpoller = zmq.Poller() zpoller.register(sys.stdin.fileno(), zmq.POLLIN) zpoller.register(z.inbox, zmq.POLLIN) def handle_stdin(): input = sys.stdin.read(1024) sys.stdin.flush() if input: z.emit_signal("stdin", input)
if __name__ == '__main__': # see https://docs.python.org/2/faq/library.html#how-do-i-get-a-single-keypress-at-a-time fd = sys.stdin.fileno() oldterm = termios.tcgetattr(fd) newattr = termios.tcgetattr(fd) newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO termios.tcsetattr(fd, termios.TCSANOW, newattr) oldflags = fcntl.fcntl(fd, fcntl.F_GETFL) fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK) hostname = socket.gethostname() z = ZOCP("keyboard@{0}".format(hostname)) z.register_string("Keyboard", "", 're') z.start() zpoller = zmq.Poller() zpoller.register(sys.stdin, zmq.POLLIN) zpoller.register(z.inbox, zmq.POLLIN) def handle_key_in(): input = sys.stdin.read(1) print("KEYBOARD IN: {0}".format(input)) z.emit_signal("Keyboard", input) running = True try: while running: items = dict(zpoller.poll()) if z.inbox in items and items[z.inbox] == zmq.POLLIN:
import fcntl import os import array import struct import pyinotify import termios from zocp import ZOCP import logging if __name__ == '__main__': logger = logging.getLogger() logger.setLevel(logging.DEBUG) logger.addHandler(logging.StreamHandler()) hostname = socket.gethostname() z = ZOCP("zinotify@{0}".format(hostname)) z.register_string("watchdir", "/tmp", "rw") z.register_string("file", "", 're') z.start() # setup inotify i = pyinotify.INotifyWrapper.create() ifd = i._inotify_init() # add a directory to watch iwd = i.inotify_add_watch( ifd, z.get_value('watchdir'), pyinotify.IN_DELETE ) zpoller = zmq.Poller() zpoller.register(ifd, zmq.POLLIN) zpoller.register(z.inbox, zmq.POLLIN) def handle_modified(peer, name, data, *args, **kwargs): global iwd