views:

121

answers:

1

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)
+2  A: 

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.

David