I have an application that sends xmpp messages. These cases are rare (sometimes none for several days), and then again, possibly, appear in bundles. I don’t want to receive anything, I just want to send. The straightforward approach works with undetected timeouts. The latter is send()not executed (the receiver does not receive anything), but returns without reporting a problem (returns a simple identifier, as if everything was working fine). Only the next call send()then raises an IOError ("Disconnected from server.").
I can do a permanent disconnect / reconnect for each message, but I don’t like it because sometimes it very often disconnects and reconnects (and I don’t know if the servers evaluated it several times per second).
I could try the approach cited as the answer in this question here, but I really don't need to receive XMPP responses.
Question: Is there an easy way to determine the connection timeout before or after sending without trying to send a second message (to spam the recipient if everything works fine)?
My direct approach:
import xmpp
def connectXmppClient(fromJidName, password):
fromJid = xmpp.protocol.JID(fromJidName)
xmppClient = xmpp.Client(fromJid.getDomain(), debug=[])
connection = xmppClient.connect()
if not connection:
raise Exception("could not setup connection", fromJid)
authentication = xmppClient.auth(
fromJid.getNode(), password, resource=fromJid.getResource())
if not authentication:
raise Exception("could not authenticate")
return xmppClient
def sendXmppMessage(xmppClient, toJidName, text):
return xmppClient.send(xmpp.protocol.Message(toJidName, text))
if __name__ == '__main__':
import sys, os, time, getpass
if len(sys.argv) < 2:
print "Syntax: xsend fromJID toJID"
sys.exit(0)
fromJidName = sys.argv[1]
toJidName = sys.argv[2]
password = getpass.getpass()
xmppClient = connectXmppClient(fromJidName, password)
while True:
line = sys.stdin.readline()
if not line:
break
print xmppClient.isConnected()
id = sendXmppMessage(xmppClient, toJidName, line)
print id
source
share