gpt4 book ai didi

python - Python 中的简单客户端/服务器 ZMQ,每个请求发送多行

转载 作者:太空狗 更新时间:2023-10-30 02:20:20 27 4
gpt4 key购买 nike

这是我第一次接触 Python 下的 ZMQ,我希望服务器在收到来自客户端的请求时发送多行。我添加到服务器端 ZMQ 提供的示例中的代码是:

with open("test.txt", 'r') as f:
for line in f:
socket.send_string(line.rstrip("\n"))

问题是如何让服务器发送所有行或者如何让客户端不在服务器之前发送请求完成从 test.txt

发送所有行

客户端

import zmq
context = zmq.Context()
print("Connecting to hello world server")
socket = context.socket(zmq.REQ)
socket.connect("tcp://localhost:5555")
for request in range(10):
print("Sending request %s" % request)
socket.send(b"Hello")
message = socket.recv()
print("Received reply %s [ %s ]" % (request, message))

服务器

import time
import zmq

context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind("tcp://*:5555")

while True:
# Wait for next request from client
message = socket.recv()
print("Received request: %s" % message)

# Do some 'work'
time.sleep(1)

# Send reply back to client
with open("test.txt", 'r') as f:
for line in f:
socket.send_string(line.rstrip("\n"))

客户端日志

Connecting to hello wolrd server
Sending request 0
Received reply 0 [ This is test line 1 ]
Sending request 1

这是它停止的地方,因为服务器生成了如下所示的错误:

服务器日志

line 324, in send_string
return self.send(u.encode(encoding), flags=flags, copy=copy)
File "socket.pyx", line 571, in zmq.backend.cython.socket.Socket.send (zmq/backend/cython/socket.c:5319)
File "socket.pyx", line 618, in zmq.backend.cython.socket.Socket.send (zmq/backend/cython/socket.c:5086)
File "socket.pyx", line 181, in zmq.backend.cython.socket._send_copy (zmq/backend/cython/socket.c:2081)
File "checkrc.pxd", line 21, in zmq.backend.cython.checkrc._check_rc (zmq/backend/cython/socket.c:6032)
zmq.error.ZMQError: Operation cannot be accomplished in current state

Process finished with exit code 1

测试.txt

This is test line 1
This is test line 2
This is test line 3
This is test line 4
This is test line 5

最佳答案

这是我想出的解决方案...以防万一有一天,有人可能需要帮助来解决类似的情况。这个想法是将所有行打包成一条消息并将其发送回客户端。似乎它的工作原理是对于 client 发出的每个请求,服务器需要做出回复并且只需要一个回复。至少我是这么看的..

在服务端替换代码

#  Send reply back to client
with open("test.txt", 'r') as f:
for line in f:
socket.send_string(line.rstrip("\n"))

与:

#  Send reply back to client
with open("test.txt", 'r') as f:
message = '%s' % f.readlines()
print(message)
print(type(message))
socket.send_string(message)

客户请求

Connecting to hello world server
Sending request 0
Received reply 0 [ ['This is test line 1\n', 'This is test line 2\n', 'This is test line 3\n', 'This is test line 4\n', 'This is test line 5\n', 'This is test line 6\n', 'This is test line 7\n', 'This is test line 8\n', 'This is test line 9\n', 'This is test line 10\n', '\n'] ]
Sending request 1
Received reply 1 [ ['This is test line 1\n', 'This is test line 2\n', 'This is test line 3\n', 'This is test line 4\n', 'This is test line 5\n', 'This is test line 6\n', 'This is test line 7\n', 'This is test line 8\n', 'This is test line 9\n', 'This is test line 10\n', '\n'] ]
....
....
and so on up to 10 requests

服务器响应

Received request: Hello
['This is test line 1\n', 'This is test line 2\n', 'This is test line 3\n', 'This is test line 4\n', 'This is test line 5\n', 'This is test line 6\n', 'This is test line 7\n', 'This is test line 8\n', 'This is test line 9\n', 'This is test line 10\n', '\n']
<type 'str'>
Received request: Hello
['This is test line 1\n', 'This is test line 2\n', 'This is test line 3\n', 'This is test line 4\n', 'This is test line 5\n', 'This is test line 6\n', 'This is test line 7\n', 'This is test line 8\n', 'This is test line 9\n', 'This is test line 10\n', '\n']
<type 'str'>
....
....
....
and so on....

既然这个问题已经解决了,下一个问题就是:客户端需要发送什么样的请求才能逐行接受来自服务器的响应。如果我有解决方案,我会更新回复,或者您可以随意参与。

关于python - Python 中的简单客户端/服务器 ZMQ,每个请求发送多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23855563/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com