使用 sleekxmpp 发送消息在进程中挂起()
Sending Messages with sleekxmpp hangs in process()
所以我得到了这个小 Python 脚本,它通过插入 PostgreSQL table 来触发并传送 XMPP 消息。工作起来很有魅力。现在升级到 Python 3.8(从 3.5)和 sleekxmpp 1.3.3。 (之前的 1.3.1)我有一个问题:它挂在 process()
.
这是我的脚本:
#!/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()
process()
的调用参见方法 send_XMPP()
。
这是我在日志文件中找到的内容 - 只是它的结尾,因为段落被一遍又一遍地重复:
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.
所以我认为“无效的证书信任链”是罪魁祸首。但是如何规避呢?
好的,看来我已经解决了-不知道失败的具体原因:
我刚刚从 sleekxmpp 版本 1.3.3 退回到版本 1.3.1。现在它像以前一样工作了。
... 仅作记录 - 刚刚发现 sleekXMPP 已被弃用 - 请参阅此处:https://github.com/fritzy/SleekXMPP
所以我得到了这个小 Python 脚本,它通过插入 PostgreSQL table 来触发并传送 XMPP 消息。工作起来很有魅力。现在升级到 Python 3.8(从 3.5)和 sleekxmpp 1.3.3。 (之前的 1.3.1)我有一个问题:它挂在 process()
.
这是我的脚本:
#!/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()
process()
的调用参见方法 send_XMPP()
。
这是我在日志文件中找到的内容 - 只是它的结尾,因为段落被一遍又一遍地重复:
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.
所以我认为“无效的证书信任链”是罪魁祸首。但是如何规避呢?
好的,看来我已经解决了-不知道失败的具体原因:
我刚刚从 sleekxmpp 版本 1.3.3 退回到版本 1.3.1。现在它像以前一样工作了。
... 仅作记录 - 刚刚发现 sleekXMPP 已被弃用 - 请参阅此处:https://github.com/fritzy/SleekXMPP