python zmq REQ和REP的请求-应答模式的代码
时间: 2024-05-02 07:20:02 浏览: 124
REP-REQ.zip_Rep和req_zmq_zmq REP_zmq REP REQ_zmq_req
Sure, here is an example of Python code for the REQ-REP pattern using ZeroMQ (zmq) library:
REQ (client) side:
```python
import zmq
context = zmq.Context()
socket = context.socket(zmq.REQ)
socket.connect("tcp://localhost:5555")
socket.send(b"Hello")
message = socket.recv()
print("Received reply:", message)
```
REP (server) side:
```python
import zmq
context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind("tcp://*:5555")
while True:
message = socket.recv()
print("Received request:", message)
socket.send(b"World")
```
In this example, the client sends a "Hello" message to the server using REQ socket, and waits for a reply. The server receives the message using the REP socket, prints it out, and sends a "World" message back to the client. The client then prints out the server's reply.
Hope this helps!
阅读全文