python-xmpp 并循环接收者列表以接收 IM 消息

发布于 2024-08-31 20:20:35 字数 1138 浏览 8 评论 0原文

我无法找出问题所在,并希望了解我的 Python 代码是否不正确,或者这是否是 Python XMPP 库的问题或设计限制。顺便说一句,我是 Python 新手。

下面是有问题的代码片段。我想要做的是读取 IM 收件人的文本文件,每行一个收件人,采用 XMPP/Jabber ID 格式。这被读入 Python 列表变量。

然后,我实例化一个 XMPP 客户端会话并循环访问收件人列表并向每个收件人发送一条消息。然后睡一会儿并重复测试。这是为了对收件人的 IM 客户端以及 IM 服务器进行负载测试。有代码可以交替处理从命令行输入而不是从文件中仅获取一个收件人的情况。

最终发生的情况是,Python 会迭代/循环列表,但只有列表中的最后一个收件人收到消息。交换收件人的顺序以进行验证。有点像 Python XMPP 库没有正确发送它,或者我错过了库调用的步骤,因为运行时的调试打印语句表明循环工作正常。


recipient = ""
delay = 60
useFile = False
recList = []
...
elif (sys.argv[i] == '-t'):
  recipient = sys.argv[i+1]
  useFile = False
elif (sys.argv[i] == '-tf'):
  fil = open(sys.argv[i+1], 'r')
  recList = fil.readlines()
  fil.close()
  useFile = True
...
# disable debug msgs
cnx = xmpp.Client(svr,debug=[])
cnx.connect(server=(svr,5223))
cnx.auth(user,pwd,'imbot')
cnx.sendInitPresence()

while (True):
  if useFile:
    for listUser in recList:
      cnx.send(xmpp.Message(listUser,msg+str(msgCounter)))
      print "sending to "+listUser+" msg = "+msg+str(msgCounter)
  else:
    cnx.send(xmpp.Message(recipient,msg+str(msgCounter)))
  msgCounter += 1
  time.sleep(delay)

I can't figure out the problem and want some input as to whether my Python code is incorrect, or if this is an issue or design limitation of Python XMPP library. I'm new to Python by the way.

Here's snippets of code in question below. What I'd like to do is read in a text file of IM recipients, one recipient per line, in XMPP/Jabber ID format. This is read into a Python list variable.

I then instantiate an XMPP client session and loop through the list of recipients and send a message to each recipient. Then sleep some time and repeat test. This is for load testing the IM client of recipients as well as IM server. There is code to alternately handle case of taking only one recipient from command line input instead of from file.

What ends up happening is that Python does iterate/loop through the list but only last recipient in list receives message. Switch order of recipients to verify. Kind of looks like Python XMPP library is not sending it out right, or I'm missing a step with the library calls, because the debug print statements during runtime indicate the looping works correctly.


recipient = ""
delay = 60
useFile = False
recList = []
...
elif (sys.argv[i] == '-t'):
  recipient = sys.argv[i+1]
  useFile = False
elif (sys.argv[i] == '-tf'):
  fil = open(sys.argv[i+1], 'r')
  recList = fil.readlines()
  fil.close()
  useFile = True
...
# disable debug msgs
cnx = xmpp.Client(svr,debug=[])
cnx.connect(server=(svr,5223))
cnx.auth(user,pwd,'imbot')
cnx.sendInitPresence()

while (True):
  if useFile:
    for listUser in recList:
      cnx.send(xmpp.Message(listUser,msg+str(msgCounter)))
      print "sending to "+listUser+" msg = "+msg+str(msgCounter)
  else:
    cnx.send(xmpp.Message(recipient,msg+str(msgCounter)))
  msgCounter += 1
  time.sleep(delay)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

讽刺将军 2024-09-07 20:20:35

没关系,找到问题了。必须注意 file.readlines() 返回的列表中元素的行末尾的换行符,因此在发送时我必须在元素上使用 .rstrip('\n') 将其删除出消息。

Never mind, found the problem. One has to watch out for the newline characters at the end of a line for the elements in a list returned by file.readlines(), so I had to strip it out with .rstrip('\n') on the element when sending out message.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文