实现单服务器响应多客户机,Python中的简单客户机/服务器ZMQ为每个请求发送多行...

这是我想出的解决办法……以防万一有一天,有人在类似的情况下可能需要帮助。

其思想是将所有行打包在一条消息中并将其发送回客户端。

它的工作原理似乎是,对于客户机发出的每个请求,服务器只需要做出一个回复,而只需要一个回复。至少我是这么看的。。

替换服务器端的代码# 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']

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']

....

....

....

and so on....

现在已经解决了这个问题,接下来的问题是:客户端需要发送什么样的请求才能以逐行方式接受服务器的响应。

如果我有解决方案,或者你可以自由参与,我会更新回复。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值