1
0
Fork 0

tools/zmqshell: port to python3

Also extend docs, use argparse and logging.
This commit is contained in:
Stefano Sabatini 2023-03-26 23:34:10 +02:00
parent 686e14e9fb
commit 774d358e0f
1 changed files with 51 additions and 10 deletions

View File

@ -1,6 +1,32 @@
#!/usr/bin/env python2 #!/usr/bin/env python3
import argparse
import cmd
import logging
import sys
import zmq
HELP = '''
Provide a shell used to send interactive commands to a zmq filter.
The command assumes there is a running zmq or azmq filter acting as a
ZMQ server.
You can send a command to it, follwing the syntax:
TARGET COMMAND [COMMAND_ARGS]
* TARGET is the target filter identifier to send the command to
* COMMAND is the name of the command sent to the filter
* COMMAND_ARGS is the optional specification of command arguments
See the zmq/azmq filters documentation for more details, and the
zeromq documentation at:
https://zeromq.org/
'''
logging.basicConfig(format='zmqshell|%(levelname)s> %(message)s', level=logging.INFO)
log = logging.getLogger()
import sys, zmq, cmd
class LavfiCmd(cmd.Cmd): class LavfiCmd(cmd.Cmd):
prompt = 'lavfi> ' prompt = 'lavfi> '
@ -14,13 +40,28 @@ class LavfiCmd(cmd.Cmd):
def onecmd(self, cmd): def onecmd(self, cmd):
if cmd == 'EOF': if cmd == 'EOF':
sys.exit(0) sys.exit(0)
print 'Sending command:[%s]' % cmd log.info(f"Sending command: {cmd}")
self.requester.send(cmd) self.requester.send_string(cmd)
message = self.requester.recv() response = self.requester.recv_string()
print 'Received reply:[%s]' % message log.info(f"Received response: {response}")
try:
bind_address = sys.argv[1] if len(sys.argv) > 1 else "tcp://localhost:5555" class Formatter(
LavfiCmd(bind_address).cmdloop('FFmpeg libavfilter interactive shell') argparse.ArgumentDefaultsHelpFormatter, argparse.RawDescriptionHelpFormatter
except KeyboardInterrupt: ):
pass pass
def main():
parser = argparse.ArgumentParser(description=HELP, formatter_class=Formatter)
parser.add_argument('--bind-address', '-b', default='tcp://localhost:5555', help='specify bind address used to communicate with ZMQ')
args = parser.parse_args()
try:
LavfiCmd(args.bind_address).cmdloop('FFmpeg libavfilter interactive shell')
except KeyboardInterrupt:
pass
if __name__ == '__main__':
main()