File: client-server.py

package info (click to toggle)
pyzmq 27.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,984 kB
  • sloc: python: 15,189; ansic: 285; makefile: 169; sh: 85
file content (24 lines) | stat: -rw-r--r-- 561 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import time

import zmq

ctx = zmq.Context.instance()

url = 'tcp://127.0.0.1:5555'
server = ctx.socket(zmq.SERVER)
server.bind(url)

for i in range(10):
    client = ctx.socket(zmq.CLIENT)
    client.connect(url)
    client.send(f'request {i}'.encode("ascii"))
    msg = server.recv(copy=False)
    print(f'server recvd {msg.bytes!r} from {msg.routing_id!r}')
    server.send_string(f'reply {i}', routing_id=msg.routing_id)
    reply = client.recv_string()
    print(f'client recvd {reply!r}')
    time.sleep(0.1)
    client.close()

server.close()
ctx.term()