Code Monkey home page Code Monkey logo

zmqc's Introduction

zmqc

zmqc is a small but powerful command-line interface to ØMQ. It allows you to create a socket of a given type, bind or connect it to multiple addresses, set options on it, and receive or send messages over it using standard I/O, in the shell or in scripts. It's useful for debugging and experimenting with most possible network topologies.

Installation

pip install zmqc

Usage

zmqc [-h] [-v] [-0] [-r | -w] (-b | -c) SOCK_TYPE [-o SOCK_OPT=VALUE...] address [address ...]

Mode

Whether to read from or write to the socket. For PUB/SUB sockets, this option is invalid since the behavior will always be write and read respectively. For REQ/REP sockets, zmqc will alternate between reading and writing as part of the request/response cycle.

-r, --read
Read messages from the socket onto stdout.
-w, --write
Write messages from stdin to the socket.

Behavior

-b, --bind
Bind to the specified address(es).
-c, --connect
Connect to the specified address(es).

Socket Parameters

SOCK_TYPE
Which type of socket to create. Must be one of `PUSH`, `PULL`, `PUB`, `SUB`, `REQ`, `REP` or `PAIR`. See `man zmq_socket` for an explanation of the different types. `DEALER` and `ROUTER` sockets are currently unsupported.
-o SOCK_OPT=VALUE, --option SOCK_OPT=VALUE
Socket option names and values to set on the created socket. Consult `man zmq_setsockopt` for a comprehensive list of options. Note that you can safely omit the `ZMQ_` prefix from the option name. If the created socket is of type `SUB`, and no `SUBSCRIBE` options are given, the socket will automatically be subscribed to everything.
address
One or more addresses to bind/connect to. Must be in full ZMQ format (e.g. `tcp://:`)

Examples

zmqc -rc SUB 'tcp://127.0.0.1:5000'

Subscribe to tcp://127.0.0.1:5000, reading messages from it and printing them to the console. This will subscribe to all messages by default (you don't need to set an empty SUBSCRIBE option). Alternatively:

zmqc -rc SUB -o SUBSCRIBE='com.organization.' 'tcp://127.0.0.1:5000'

This will subscribe to all messages starting with com.organization..


ls | zmqc -wb PUSH 'tcp://*:4000'

Send the name of every file in the current directory as a message from a PUSH socket bound to port 4000 on all interfaces. Don't forget to quote the address to avoid glob expansion.


zmqc -rc PULL 'tcp://127.0.0.1:5202' | tee $TTY | zmqc -wc PUSH 'tcp://127.0.0.1:5404'

Read messages coming from a PUSH socket bound to port 5202 (note that we're connecting with a PULL socket), echo them to the active console, and forward them to a PULL socket bound to port 5404 (so we're connecting with a PUSH).


zmqc -n 10 -0rb PULL 'tcp://*:4123' | xargs -0 grep 'pattern'

Bind to a PULL socket on port 4123, receive 10 messages from the socket (with each message representing a filename), and grep the files for 'pattern'. The -0 option means messages will be NULL-delimited rather than separated by newlines, so that filenames with spaces in them are not considered two separate arguments by xargs.


echo "hello" | zmqc -c REQ 'tcp://127.0.0.1:4000'

Send the string hello through a REQ socket connected to localhost on port 4000, print whatever you get back, and finish. In this way, REQ sockets can be used for a rudimentary form of RPC in shell scripts.


coproc zmqc -b REP 'tcp://*:4000'
tr -u '[a-z]' '[A-Z]' <&p >&p &
echo "hello" | zmqc -c REQ 'tcp://127.0.0.1:4000'

First, start a REP socket listening on port 4000. The coproc shell command runs this as a shell coprocess, which allows us to run the next line, tr. This will read its input from the REP socket's output, translate all lowercase characters to uppercase, and send them back to the REP socket's input. This, again, is run in the background. Finally, connect a REQ socket to that REP socket and send the string hello through it: you should just see the string HELLO printed on stdout.

(Un)license

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.

In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to http://unlicense.org/

zmqc's People

Contributors

vlcinsky avatar zacharyvoase avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

zmqc's Issues

AttributeError: 'module' object has no attribute 'core' with pyzmq 15.2.0

Hi,

I installed zmqc with pip

sudo pip install zmqc

It was installed correctly

zmqc
usage: zmqc [-h] [-v] [-0] [-r | -w] (-b | -c)
            SOCK_TYPE [-o SOCK_OPT=VALUE...]
            address [address ...]
zmqc: error: too few arguments

But when using the tool I got the stacktrace :

zmqc -rb SUB 'tcp://127.0.0.1:5557'                    
Traceback (most recent call last):
  File "/usr/local/bin/zmqc", line 11, in <module>
    load_entry_point('zmqc==0.1.0', 'console_scripts', 'zmqc')()
  File "/usr/local/lib/python2.7/dist-packages/zmqc.py", line 286, in main
    sock_opts = get_sockopts(args.sock_opts)
  File "/usr/local/lib/python2.7/dist-packages/zmqc.py", line 227, in get_sockopts
    int: set(zmq.core.constants.int_sockopts).union(
AttributeError: 'module' object has no attribute 'core'

Here are my zmq / pyzmq and python versions

ipython
iPython 2.7.6 (default, Jun 22 2015, 17:58:13) 
Type "copyright", "credits" or "license" for more information.

IPython 5.0.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import zmq

In [2]: zmq.zmq_version
Out[2]: <function zmq.sugar.version.zmq_version>

In [3]: zmq.zmq_version()
Out[3]: '4.1.2'

In [4]: zmq.pyzmq_version
Out[4]: <function zmq.sugar.version.pyzmq_version>

In [5]: zmq.pyzmq_version()
Out[5]: '15.2.0'

Add tests to protect against refactors/new features/regressions.

A few considerations:

We'd like end-to-end tests but we also want to mock out certain function calls (mainly so we can make assertions about the mapping from command-line arguments -> API methods). Is there a standard/accepted way of mocking libraries just in a subprocess?

zmqc: error: Unrecognised socket option: 'SUBSCRIBE' for pyzmq version 2.x

With pyzmq==2.* it reports an error:

zmqc: error: Unrecognised socket option: 'SUBSCRIBE'

It can be seen by:

$ tox -e py27-pyzmq2

Possible solutions

There are two options

  • stop supporting pyzmq < 13.0
  • provide SUBSCRIBE option only for later versions of pyzmq.

Option: stop supporting pyzmq 2.x

Sounds simple, just updating install_requires.

However, this might break some older installs.

Provide SUBSCRIBE option only for later versions of pyzmq.

It would be nice, but mess up the code a bit.

Python 3 compatibility

Currently it is incompatible:

$ zmqc -rc SUB 'tcp://*:6558'
Traceback (most recent call last):
  File "/home/akochkov/.local/bin/zmqc", line 11, in <module>
    load_entry_point('zmqc==0.1.1', 'console_scripts', 'zmqc')()
  File "/home/akochkov/.local/lib/python3.6/site-packages/zmqc-0.1.1-py3.6.egg/zmqc.py", line 302, in main
  File "zmq/backend/cython/socket.pyx", line 374, in zmq.backend.cython.socket.Socket.set (zmq/backend/cython/socket.c:4619)
TypeError: unicode not allowed, use setsockopt_string

Then I changed line 302 to use b'' instead of '' for SUBSCRIBE operation, but got a new error:

$ zmqc -rc SUB 'tcp://*:6558'
Traceback (most recent call last):
  File "/home/akochkov/.local/bin/zmqc", line 11, in <module>
    load_entry_point('zmqc==0.1.1', 'console_scripts', 'zmqc')()
  File "/home/akochkov/.local/lib/python3.6/site-packages/zmqc-0.1.1-py3.6.egg/zmqc.py", line 306, in main
  File "zmq/backend/cython/socket.pyx", line 528, in zmq.backend.cython.socket.Socket.connect (zmq/backend/cython/socket.c:5980)
  File "zmq/backend/cython/checkrc.pxd", line 25, in zmq.backend.cython.checkrc._check_rc (zmq/backend/cython/socket.c:8400)
zmq.error.ZMQError: Invalid argument

Support for REQ / REP sockets

Hello,

Just saw this on Hacker News - very cool project! Are you planning on adding support for REQ / REP sockets? If there's any interest, I might add it myself and submit a pull request, unless there's some compelling reason that they can't be supported?

Best Regards.

Too old distribute package dependency?

$ pip3 install --user zmqc
Collecting zmqc
  Using cached zmqc-0.1.0.tar.gz
    Complete output from command python setup.py egg_info:
    Downloading http://pypi.python.org/packages/source/d/distribute/distribute-0.6.24.tar.gz
    Traceback (most recent call last):
      File "/tmp/pip-build-uh7xyrn8/zmqc/distribute_setup.py", line 143, in use_setuptools
        raise ImportError
    ImportError
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-build-uh7xyrn8/zmqc/setup.py", line 5, in <module>
        use_setuptools()
      File "/tmp/pip-build-uh7xyrn8/zmqc/distribute_setup.py", line 145, in use_setuptools
        return _do_download(version, download_base, to_dir, download_delay)
      File "/tmp/pip-build-uh7xyrn8/zmqc/distribute_setup.py", line 124, in _do_download
        to_dir, download_delay)
      File "/tmp/pip-build-uh7xyrn8/zmqc/distribute_setup.py", line 193, in download_setuptools
        src = urlopen(url)
      File "/usr/lib64/python3.6/urllib/request.py", line 223, in urlopen
        return opener.open(url, data, timeout)
      File "/usr/lib64/python3.6/urllib/request.py", line 532, in open
        response = meth(req, response)
      File "/usr/lib64/python3.6/urllib/request.py", line 642, in http_response
        'http', request, response, code, msg, hdrs)
      File "/usr/lib64/python3.6/urllib/request.py", line 570, in error
        return self._call_chain(*args)
      File "/usr/lib64/python3.6/urllib/request.py", line 504, in _call_chain
        result = func(*args)
      File "/usr/lib64/python3.6/urllib/request.py", line 650, in http_error_default
        raise HTTPError(req.full_url, code, msg, hdrs, fp)
    urllib.error.HTTPError: HTTP Error 403: SSL is required
    
    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-uh7xyrn8/zmqc/

Same happens with pip for Python 2.

Set vlcinsky as Maintainer on PyPi

@zacharyvoase

In PR #7 I have asked to get permission to maintain the zmqc package on pypi (and seemed to be willing to do that).

To give it better visibility (discussing in closed PR is not the best way to ask for an action), I am creating this issue.

To set another person as Owner or Maintainer of PyPi package

To allow myself updating zmqc on pypi, the process goes as follows:

  1. go to https://pypi.python.org
  2. log into your account
  3. pick the zmqc package (right top part of the screen)
  4. select option "roles"
  5. add myself (as vlcinsky) with "Maintainer" role.
  6. submit
  7. let me know you are done

Testing the role procedure on bflmpsvz package

I did test this procedure (using my accounts vlcinsky and jan.vlcinsky on package https://pypi.python.org/pypi/bflmpsvz/0.1.1

If you like, you may clone the package from https://github.com/vlcinsky/bflmpsvz, update and publish an update (but before let me know your account name on pypi)

On PyPi I have tried giving access to bflmpsvz for disturbyte, zacharyvoase and zachary.voase but all have reported "user name not known".

My plans with zmqc package on pypi

  1. publish version zmqc 0.1.1 (see master tip of this repository where it is already pulled)
  2. Possibly make the package compatible with Python 3
  3. Possibly convert the setup.py to use pbr (to make versioning based on git tags)

All the time I want to:

  1. keep the UNLICENSE "License"
  2. keep you as package author (possibly adding myself somewhere around).

Let me know if you have any objections, want me to consult with you all these changes or you simply let me follow up with the great zmqc package in any way I find practical without bothering you.

REQ to ROUTER connection

Very cool project!
I am trying to figure out why is following not supported:

  1. Router listening at tcp://*.5570
  2. zmqc sending a message via a REQ socket:
  echo "Hello" | zmqc -c REQ 'tcp://127.0.0.1:5570'

I am getting the following error message:

Traceback (most recent call last):
  File "C:\Python27\Scripts\zmqc-script.py", line 8, in <module>
    load_entry_point('zmqc==0.1.0', 'console_scripts', 'zmqc')()
  File "C:\Python27\lib\site-packages\zmqc.py", line 302, in main
    getattr(sock, args.behavior)(address)
  File "socket.pyx", line 475, in zmq.core.socket.Socket.connect (zmq\core\socket.c:4508)
zmq.core.error.ZMQError: Protocol not supported

Any ideas?

refactor `setup.py` to use `pbr`

Minor problems with current setup.py

Existing setup.py has couple of minor problems:

  • not easy to read
  • not easy to maintain version through development

How can pbr package change it

There is a package called pbr, used by OpenStack project, which offers working solution:

  • setup.py becomes 3-line file
  • all package specific parameters are move to setup.cfg
  • versions are derived from tags in git repository (and can be set via environmental variables, if used out of git repository)

Apart from that, package get automatically generated files, included in distribution package:

  • AUTHORS: listing all users who committed changes
  • ChangeLog: list of changes made from commit messages (first lines), organized into groups related to versions

Proposed workflow

Assuming we have setup.py converted for pbr, we shall follow these rules:

  • when cloning and updating git repository, we shall pull and push tags
  • developing zmqc is not affected at all
  • at the moment some commit seems to be ready for release (and is well tested), we shall attach tag with version (e.g. 0.1.2) to related commit. Buidling sdist from such commit will result in package with given release.
  • further commit (during development) get automatically incremented version (like 0.1.3dev1)

missing dependencies?

Hi
I am running Ubuntu 64 bit (with no previous Python installations).. so I installed...
$ sudo apt-get install python-pip

$ sudo pip install zmqc
Downloading/unpacking zmqc
Running setup.py egg_info for package zmqc

Requirement already satisfied (use --upgrade to upgrade): argparse>=1.2.1 in /usr/lib/python2.7 (from zmqc)
Installing collected packages: zmqc
Running setup.py install for zmqc

Installing zmqc script to /usr/local/bin

Successfully installed zmqc
Cleaning up...

$ zmqc
Traceback (most recent call last):
File "/usr/local/bin/zmqc", line 9, in
load_entry_point('zmqc==0.1.0', 'console_scripts', 'zmqc')()
File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 337, in load_entry_point
return get_distribution(dist).load_entry_point(group, name)
File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 2311, in load_entry_point
return ep.load()
File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 2017, in load
entry = import(self.module_name, globals(),globals(), ['name'])
File "/usr/local/lib/python2.7/dist-packages/zmqc.py", line 86, in
import zmq
ImportError: No module named zmq

what did I miss?

thanks
Nishant

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.