So I've got this little Python script that get's triggerd from an insert into a PostgreSQL table and deliveres XMPP Messages. Worked like a charm. Now with an upgrade to Python 3.8 (from 3.5) and sleekxmpp 1.3.3. (previous 1.3.1) I have an issue: it hangs in process()
.
Here's my script:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import psycopg2.extensions
import select
import sys
import logging
import getpass
from optparse import OptionParser
import sleekxmpp
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
import django
django.setup()
from unplug.models import Xmpp
from django.db import connection
cursor=connection.cursor()
pg_con=connection.connection
logging.basicConfig(filename='sendXMPPpy.log', level=logging.DEBUG)
if sys.version_info < (3, 0):
reload(sys)
sys.setdefaultencoding('utf8')
else:
raw_input = input
class SendMsgBot(sleekxmpp.ClientXMPP):
def __init__(self, jid, password, recipient, message):
sleekxmpp.ClientXMPP.__init__(self, jid, password)
self.recipient = recipient
self.msg = message
self.add_event_handler("session_start", self.start)
def start(self, event):
self.send_presence()
self.get_roster()
if self.msg =="":
# bei leerer Message wird er Empfaenger gebucht ...
self.update_roster(recipient,block=False,subscription="to")
else:
self.send_message(mto=self.recipient,
mbody=self.msg,
mtype='chat')
self.disconnect(wait=True)
def send_XMPP(target,message):
print("send_XMPP()")
xmpp = SendMsgBot("YYY@jabber.de", "XXX", target,message)
xmpp.register_plugin('xep_0030') # Service Discovery
xmpp.register_plugin('xep_0199') # XMPP Ping
if xmpp.connect():
xmpp.process(block=True) # here it hangs!
print("Done")
else:
print("Unable to connect.")
if __name__ == '__main__':
cursor.execute('LISTEN xmpp')
while 1:
if select.select([pg_con],[],[],5) == ([],[],[]):
pass
else:
pg_con.poll()
while pg_con.notifies:
notify = pg_con.notifies.pop(0)
for sx in Xmpp.objects.all():
send_XMPP(sx.target,' ' if sx.mBody == '' else sx.mBody)
sx.delete()
See method send_XMPP()
for the call of process()
.
This is what I find in the log file - just the end of it, as the passage is repeated over and over:
DEBUG:sleekxmpp.xmlstream.xmlstream:Event triggered: session_end
DEBUG:sleekxmpp.xmlstream.xmlstream:Event triggered: disconnected
DEBUG:sleekxmpp.thirdparty.statemachine: ==== TRANSITION connected -> disconnected
ERROR:sleekxmpp.xmlstream.xmlstream:Can not read from closed socket.
DEBUG:sleekxmpp.xmlstream.xmlstream:reconnecting...
DEBUG:sleekxmpp.xmlstream.xmlstream:connecting...
DEBUG:sleekxmpp.xmlstream.xmlstream:Waiting 2.1448919023898796 seconds before connecting.
DEBUG:sleekxmpp.xmlstream.xmlstream:No remaining DNS records to try.
DEBUG:sleekxmpp.xmlstream.xmlstream:Waiting 3.819744501097285 seconds before connecting.
WARNING:sleekxmpp.xmlstream.resolver:DNS: dnspython not found. Can not use SRV lookup.
DEBUG:sleekxmpp.xmlstream.resolver:DNS: Querying jabber.de for AAAA records.
DEBUG:sleekxmpp.xmlstream.resolver:DNS: Querying jabber.de for A records.
DEBUG:sleekxmpp.xmlstream.xmlstream:Connecting to [2a01:238:42b4:2600:a44f:27fe:6a8a:cd3c]:5222
DEBUG:sleekxmpp.xmlstream.xmlstream:Event triggered: connected
DEBUG:sleekxmpp.thirdparty.statemachine: ==== TRANSITION disconnected -> connected
DEBUG:sleekxmpp.xmlstream.xmlstream:SEND (IMMED): <stream:stream to='jabber.de' xmlns:stream='http://etherx.jabber.org/streams' xmlns='jabber:client' xml:lang='en' version='1.0'>
DEBUG:sleekxmpp.xmlstream.xmlstream:RECV: <stream:stream from="jabber.de" version="1.0" id="dcdb99e5-ef47-4a89-8925-522617c39432" xml:lang="en">
DEBUG:sleekxmpp.xmlstream.xmlstream:RECV: <stream:features xmlns="http://etherx.jabber.org/streams"><starttls xmlns="urn:ietf:params:xml:ns:xmpp-tls"><required /></starttls></stream:features>
DEBUG:sleekxmpp.xmlstream.xmlstream:SEND (IMMED): <starttls xmlns="urn:ietf:params:xml:ns:xmpp-tls"><required /></starttls>
DEBUG:sleekxmpp.xmlstream.xmlstream:RECV: <proceed xmlns="urn:ietf:params:xml:ns:xmpp-tls" />
DEBUG:sleekxmpp.features.feature_starttls.starttls:Starting TLS
INFO:sleekxmpp.xmlstream.xmlstream:Negotiating TLS
INFO:sleekxmpp.xmlstream.xmlstream:Using SSL version: TLSv1
ERROR:sleekxmpp.xmlstream.xmlstream:CERT: Invalid certificate trust chain.
DEBUG:sleekxmpp.xmlstream.xmlstream:Event triggered: session_end
DEBUG:sleekxmpp.xmlstream.xmlstream:Event triggered: disconnected
DEBUG:sleekxmpp.thirdparty.statemachine: ==== TRANSITION connected -> disconnected
ERROR:sleekxmpp.xmlstream.xmlstream:Can not read from closed socket.
DEBUG:sleekxmpp.xmlstream.xmlstream:reconnecting...
DEBUG:sleekxmpp.xmlstream.xmlstream:connecting...
DEBUG:sleekxmpp.xmlstream.xmlstream:Waiting 2.2652390846224826 seconds before connecting.
DEBUG:sleekxmpp.xmlstream.xmlstream:Stopped event runner thread. 2 threads remain.
DEBUG:sleekxmpp.xmlstream.xmlstream:Stopped send thread. 1 threads remain.
DEBUG:sleekxmpp.xmlstream.scheduler:Quitting Scheduler thread
DEBUG:sleekxmpp.xmlstream.xmlstream:Stopped scheduler thread. 0 threads remain.
So I gather an "Invalid certificate trust chain" is the culprit. But how to circumvent this?
Ok, seems I have solved it - without knowing the specific reason of the failure:
I just stepped back from sleekxmpp version 1.3.3 to version 1.3.1. And now it workes as before.
... and just for the record - just found out that sleekXMPP is deprecated - see here: https://github.com/fritzy/SleekXMPP