async def send_and_recv(): address = 'tcp://127.0.0.1:13131' # in real code you should also pass recv_timeout and/or send_timeout with Pair0(listen=address) as s0, Pair0(dial=address) as s1: await s0.asend(b'hello s1') print(await s1.arecv()) # prints b'hello s1' await s1.asend(b'hi old buddy s0, great to see ya') print(await s0.arecv()) # prints b'hi old buddy s0, great to see ya
def hello_nng(): from pynng import Pair0 peer_one = Pair0() peer_one.listen('tcp://127.0.0.1:54321') peer_two = Pair0() peer_two.dial('tcp://127.0.0.1:54321') peer_one.send(b'Well hello there') print(peer_two.recv()) peer_one.close() peer_two.close()
def test_config_string(): with Pair0(recv_timeout=1000, send_timeout=1000) as server, \ Pair0(recv_timeout=1000, send_timeout=1000) as client: c_server = TLSConfig(TLSConfig.MODE_SERVER, own_key_string=SERVER_KEY, own_cert_string=SERVER_CERT) server.tls_config = c_server c_client = TLSConfig(TLSConfig.MODE_CLIENT, ca_string=CA_CERT) client.tls_config = c_client server.listen(URL) client.dial(URL) client.send(BYTES) assert server.recv() == BYTES server.send(BYTES) assert client.recv() == BYTES
def test_config_file(tmp_path): ca_crt_file = tmp_path / "ca.crt" ca_crt_file.write_text(CA_CERT) key_pair_file = tmp_path / "key_pair_file.pem" key_pair_file.write_text(SERVER_CERT + SERVER_KEY) with Pair0(recv_timeout=1000, send_timeout=1000) as server, \ Pair0(recv_timeout=1000, send_timeout=1000) as client: c_server = TLSConfig(TLSConfig.MODE_SERVER, cert_key_file=str(key_pair_file)) server.tls_config = c_server c_client = TLSConfig(TLSConfig.MODE_CLIENT, ca_files=[str(ca_crt_file)]) client.tls_config = c_client server.listen(URL) client.dial(URL) client.send(BYTES) assert server.recv() == BYTES server.send(BYTES) assert client.recv() == BYTES
from pynng import Pair0 address = 'ws://127.0.0.1:7789' pair = Pair0(dial=address) pair.send(b'hello') print(pair.recv())
from pynng import Pair0 address = 'tcp://127.0.0.1:13131' # in real code you should also pass recv_timeout and/or send_timeout with Pair0(listen=address) as s0, Pair0(dial=address) as s1: s0.send(b'hello s1') print(s1.recv()) # prints b'hello s1' s1.send(b'hi old buddy s0, great to see ya') print(s0.recv()) # prints b'hi old buddy s0, great to see ya
from pynng import Pair0, Pair1 address = 'ws://127.0.0.1:7789' pair = Pair0(listen=address) print(pair.recv()) pair.send(b'world')
#!/usr/bin/env python3 # -*- coding: utf-8 -*- from pynng import Pair0 s1 = Pair0() s1.listen("ipc://127.0.0.1:54321") s2 = Pair0() s2.dial("ipc://127.0.0.1:54321") s1.send(b"Well hello there from s1") print(s2.recv()) s2.send(b"Well hello there from s2") print(s1.recv()) s1.close() s2.close()