我使用xmpppy http://xmpppy.sourceforge.net/發送Jabber的通知,這是使用下面的代碼單個目標運行良好:如何使用XMPP和Python xmpppy發送多播消息(多用戶)(XEP-0033:擴展詩節尋址)
# pip install https://github.com/rochacbruno/xmpppy/tarball/master
import xmpp
JABBER_SETTINGS = {"USERNAME": None, "PASSWORD": None, "DOMAIN": None, "RESOURCE": None}
def get_jabber_client():
client = xmpp.Client(JABBER_SETTINGS.get('DOMAIN'))
client.connect(server=(JABBER_SETTINGS.get('DOMAIN'), '5222'))
client.auth(
JABBER_SETTINGS.get('USERNAME'),
JABBER_SETTINGS.get('PASSWORD'),
JABBER_SETTINGS.get('RESOURCE')
)
client.sendInitPresence()
return client
def send_message(to, message):
client = get_jabber_client()
xmpp_message = xmpp.Message(to, message)
client.send(xmpp_message)
client.disconnect()
send_message("[email protected]", "Hello World!")
但現在我需要發送消息到多個目的地,現在我正在做。
for users in list_of_users:
send_message(user, "Hello World!")
哪一個可以正常工作,但每次我打電話時都會啓動認證過程並花費大量時間。
我試過創建一個客戶端並使用相同的客戶端發送消息。
def send_message(to, message):
if isinstance(to, basestring):
to = [to]
assert isinstance(to, (list, tuple))
client = get_jabber_client()
for destination in to:
xmpp_message = xmpp.Message(destination, message)
client.send(xmpp_message)
client.disconnect()
send_message(['user1...', 'user2...'], "Hello World!")
上述工程的代碼,而是僅在列表中的第一個用戶獲取以及格式化該消息,其他用戶在純XML接收該消息。
我看到這個代碼(.NET),提XEP-0033:擴展尋址詩節http://forum.ag-software.net/thread/1482-Send-Message-To-all-users-in-contact-list
var addresses = new Addresses();
addresses.AddAddress(new Address
{
Type = Type.to,
Jid = "[email protected]/Work",
Description = "Joe Hildebrand"
});
addresses.AddAddress(new Address
{
Type = Type.cc,
Jid = "[email protected]/Home",
Description = "Jeremie Miller"
});
var msg = new Matrix.Xmpp.Client.Message();
msg.Add(addresses);
msg.To = "multicast.jabber.org";
msg.Body = "Hello, world!";
builds the following Xml:
<message to='multicast.jabber.org'>
<addresses xmlns='http://jabber.org/protocol/address'>
<address type='to' jid='[email protected]/Work' desc='Joe Hildebrand'/>
<address type='cc' jid='[email protected]/Home' desc='Jeremie Miller'/>
</addresses>
<body>Hello, world!</body>
</message>
但我沒有發現使用xmpppy做同樣的Python中,如何任何想法構建組播節並使用Python將消息發送給多個用戶?
謝謝。
我糾正變量名,但仍然沒有不工作,我覺得還有別的。更好的方法是實施XEP-0033。謝謝 – 2015-02-24 13:40:08