Skip to main content

Networking made simply using nng

Project description

This is pynng.

MIT License PyPI Version Linux Status Windows Status docs

Ergonomic bindings for nanomsg next generation (nng), in Python. pynng provides a nice interface on top of the full power of nng. nng, and therefore pynng, make it easy to communicate between processes on a single computer or computers across a network. This library is compatible with Python ≥ 3.5. nng is the rewriting of Nanomsg, which is the spiritual successor to ZeroMQ.

Goals

Provide a Pythonic, works-out-of-the box library on Windows and Unix-y platforms. Like nng itself, the license is MIT, so it can be used without restriction.

Installation

On Windows, MacOS, and Linux, the usual

pip3 install pynng

should suffice. Note that on 32-bit Linux and on macOS no binary distributions are available, so CMake is also required.

Building from the GitHub repo works as well, natch:

git clone https://github.com/codypiersall/pynng
cd pynng
pip3 install -e .

(If you want to run tests, you also need to pip3 install pytest and pip3 install trio, then just run pytest.)

pynng might work on the BSDs as well. Who knows!

Using pynng

Using pynng is easy peasy:

from pynng import Pair0

s1 = Pair0()
s1.listen('tcp://127.0.0.1:54321')
s2 = Pair0()
s2.dial('tcp://127.0.0.1:54321')
s1.send(b'Well hello there')
print(s2.recv())
s1.close()
s2.close()

Since pynng sockets support setting most parameters in the socket's __init__ method and is a context manager, the above code can be written much shorter:

from pynng import Pair0

with Pair0(listen='tcp://127.0.0.1:54321') as s1, \
        Pair0(dial='tcp://127.0.0.1:54321') as s2:
    s1.send(b'Well hello there')
    print(s2.recv())

Using pynng with an async framework

Asynchronous sending also works with

trio and asyncio. Here is an example using trio:

import pynng
import trio

async def send_and_recv(sender, receiver, message):
    await sender.asend(message)
    return await receiver.arecv()

with pynng.Pair0(listen='tcp://127.0.0.1:54321') as s1, \
        pynng.Pair0(dial='tcp://127.0.0.1:54321') as s2:
    received = trio.run(send_and_recv, s1, s2, b'hello there old pal!')
    assert received == b'hello there old pal!'

Many other protocols are available as well:

  • Pair0: one-to-one, bidirectional communication.
  • Pair1: one-to-one, bidirectional communication, but also supporting polyamorous sockets
  • Pub0, Sub0: publish/subscribe sockets.
  • Surveyor0, Respondent0: Broadcast a survey to respondents, e.g. to find out what services are available.
  • Req0, Rep0: request/response pattern.
  • Push0, Pull0: Aggregate messages from multiple sources and load balance among many destinations.

Feel free to check out the documentation, which is currently a work in progress.

Examples

Some examples (okay, just two examples) are available in the examples directory.

Git Branch Policy

The only stable branch is master. There will never be a git push -f on master. On the other hand, all other branches are not considered stable; they may be deleted, rebased, force-pushed, and any other manner of funky business.

TODO

  • More docs. Most of the public API has docstrings, but hosted documentation at readthedocs will happen one day.

Project details


Release history Release notifications

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for pynng-tls, version 0.4.1
Filename, size File type Python version Upload date Hashes
Filename, size pynng_tls-0.4.1-cp35-cp35m-macosx_10_6_intel.whl (528.1 kB) File type Wheel Python version cp35 Upload date Hashes View hashes
Filename, size pynng_tls-0.4.1-cp35-cp35m-manylinux2010_i686.whl (607.0 kB) File type Wheel Python version cp35 Upload date Hashes View hashes
Filename, size pynng_tls-0.4.1-cp35-cp35m-manylinux2010_x86_64.whl (609.8 kB) File type Wheel Python version cp35 Upload date Hashes View hashes
Filename, size pynng_tls-0.4.1-cp35-cp35m-manylinux2014_i686.whl (587.7 kB) File type Wheel Python version cp35 Upload date Hashes View hashes
Filename, size pynng_tls-0.4.1-cp35-cp35m-manylinux2014_x86_64.whl (603.9 kB) File type Wheel Python version cp35 Upload date Hashes View hashes
Filename, size pynng_tls-0.4.1-cp35-cp35m-win32.whl (328.6 kB) File type Wheel Python version cp35 Upload date Hashes View hashes
Filename, size pynng_tls-0.4.1-cp35-cp35m-win_amd64.whl (392.9 kB) File type Wheel Python version cp35 Upload date Hashes View hashes
Filename, size pynng_tls-0.4.1-cp36-cp36m-macosx_10_6_intel.whl (528.1 kB) File type Wheel Python version cp36 Upload date Hashes View hashes
Filename, size pynng_tls-0.4.1-cp36-cp36m-manylinux2010_i686.whl (606.6 kB) File type Wheel Python version cp36 Upload date Hashes View hashes
Filename, size pynng_tls-0.4.1-cp36-cp36m-manylinux2010_x86_64.whl (609.3 kB) File type Wheel Python version cp36 Upload date Hashes View hashes
Filename, size pynng_tls-0.4.1-cp36-cp36m-manylinux2014_i686.whl (587.7 kB) File type Wheel Python version cp36 Upload date Hashes View hashes
Filename, size pynng_tls-0.4.1-cp36-cp36m-manylinux2014_x86_64.whl (603.6 kB) File type Wheel Python version cp36 Upload date Hashes View hashes
Filename, size pynng_tls-0.4.1-cp36-cp36m-win32.whl (329.1 kB) File type Wheel Python version cp36 Upload date Hashes View hashes
Filename, size pynng_tls-0.4.1-cp36-cp36m-win_amd64.whl (392.4 kB) File type Wheel Python version cp36 Upload date Hashes View hashes
Filename, size pynng_tls-0.4.1-cp37-cp37m-macosx_10_6_intel.whl (528.1 kB) File type Wheel Python version cp37 Upload date Hashes View hashes
Filename, size pynng_tls-0.4.1-cp37-cp37m-manylinux2010_i686.whl (606.6 kB) File type Wheel Python version cp37 Upload date Hashes View hashes
Filename, size pynng_tls-0.4.1-cp37-cp37m-manylinux2010_x86_64.whl (609.3 kB) File type Wheel Python version cp37 Upload date Hashes View hashes
Filename, size pynng_tls-0.4.1-cp37-cp37m-manylinux2014_i686.whl (587.7 kB) File type Wheel Python version cp37 Upload date Hashes View hashes
Filename, size pynng_tls-0.4.1-cp37-cp37m-manylinux2014_x86_64.whl (603.6 kB) File type Wheel Python version cp37 Upload date Hashes View hashes
Filename, size pynng_tls-0.4.1-cp37-cp37m-win32.whl (329.1 kB) File type Wheel Python version cp37 Upload date Hashes View hashes
Filename, size pynng_tls-0.4.1-cp37-cp37m-win_amd64.whl (392.4 kB) File type Wheel Python version cp37 Upload date Hashes View hashes
Filename, size pynng_tls-0.4.1-cp38-cp38-macosx_10_9_x86_64.whl (456.9 kB) File type Wheel Python version cp38 Upload date Hashes View hashes
Filename, size pynng_tls-0.4.1-cp38-cp38-manylinux2010_i686.whl (608.1 kB) File type Wheel Python version cp38 Upload date Hashes View hashes
Filename, size pynng_tls-0.4.1-cp38-cp38-manylinux2010_x86_64.whl (611.1 kB) File type Wheel Python version cp38 Upload date Hashes View hashes
Filename, size pynng_tls-0.4.1-cp38-cp38-manylinux2014_i686.whl (588.5 kB) File type Wheel Python version cp38 Upload date Hashes View hashes
Filename, size pynng_tls-0.4.1-cp38-cp38-manylinux2014_x86_64.whl (604.8 kB) File type Wheel Python version cp38 Upload date Hashes View hashes
Filename, size pynng_tls-0.4.1-cp38-cp38-win32.whl (329.1 kB) File type Wheel Python version cp38 Upload date Hashes View hashes
Filename, size pynng_tls-0.4.1-cp38-cp38-win_amd64.whl (392.5 kB) File type Wheel Python version cp38 Upload date Hashes View hashes
Filename, size pynng-tls-0.4.1.tar.gz (643.7 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page