slimxmpp 线程身份验证 [英] sleekxmpp threaded authentication

查看:19
本文介绍了slimxmpp 线程身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以...我有一个像这样的简单聊天客户端:

so... I have a simple chat client like so:

class ChatClient(sleekxmpp.ClientXMPP):
    def __init__(self, jid, password, server):
        sleekxmpp.ClientXMPP.__init__(self, jid, password, ssl=True)

        self.add_event_handler("session_start", self.start)

        self.register_plugin('xep_0030')
        self.register_plugin('xep_0004')
        self.register_plugin('xep_0060')
        self.register_plugin('xep_0199')

        self.ssl_version = ssl.PROTOCOL_SSLv3
        self.connected = self.connect()
        if self.connected:
            self.process(threaded=True)


    def start(self, event):
        self.send_presence(priority = "-9001")
        self.get_roster(blocking = True, timeout = 3)

    def message(self, targets, msg):
        for target in targets:
            self.send_message(target, msg)

我有一个验证"功能,以确保您输入的用户名/密码正确:

and I have an "verify" function to make sure you input your username/pass right:

def authenticate(username, password, server):
    xmppuser = username + '@' + server
    passTester = ChatClient(xmppuser, password)

    passTester.disconnect(wait = True)
    authenticated = passTester.authenticated
    return authenticated

现在,问题出现在我将聊天客户端设为线程的情况下,我遇到了在服务器有机会实际连接之前尝试检查 ChatClient.authenticated 的情况.如您所见,我尝试在断开连接时等待",但发送队列中没有任何内容,因此它立即断开连接.

Now, the problem comes in where I have the chat client as threaded, I run into the situation where I try to check ChatClient.authenticated before the server had a chance to actually connect. As you can see, I tried to "wait" on the disconnect but there's nothing in the send queue so it disconnects right away.

我尝试过的另一种方法是:

An alternate I tried is this:

def authenticate(username, password, server):
    xmppuser = username + '@' + server
    passTester = ChatClient(xmppuser, password)

    passTester.message('bogusName', 'ladfhkjdglkhjdfg')
    passTester.disconnect(wait = True)
    authenticated = passTester.authenticated
    return authenticated

现在我发送了一条虚假消息,断开呼叫需要等待.当我使用此代码输入正确的用户名/密码时,断开连接等待发送消息(包括等待真正的连接),不发送任何内容并且 ChatClient.authenticated 设置为 True!

Now that I sent a bogus message the disconnect call has something to wait for. when I input a correct username/pass with this code, the disconnect waits for a message to get sent (which involves waiting for a real connection), sends nothing and ChatClient.authenticated is set to True!

不幸的是,当我输入错误的用户名/通过时,消息永远不会被发送,并且断开连接(wait=True)永远不会断开连接,因为消息永远不会被发送.

Unfortunately, when I input a wrong username/pass the message never gets sent and the disconnect(wait=True) never disconnects as the message never gets sent.

有没有其他人有更合适的方式来认证"?

Does anyone else have a more proper way to "authenticate"?

推荐答案

这将是将 .authenticated 和相关字段更改为 threading.Event 的一个很好的理由对象,以便您可以在这种情况下使用 wait(),但我不确定这会破坏现有用户代码的程度.

This would be a good reason for changing the .authenticated and related fields to be threading.Event objects so that you could use wait() for situations like this, but I'm not sure how much that would break existing user code.

但是除了修改 SleekXMPP 之外,您需要做的是等待某些事件触发.例如,如果您的客户端成功通过身份验证,那么将会有一个 session_start 事件(我稍后可能会添加一个 auth_success 或类似事件).同样,如果单个机制的身份验证失败,则会出现 failed_auth 事件.如果根本没有任何身份验证方法成功(这是您感兴趣的),则会发生 no_auth 事件.

But short of modifying SleekXMPP, what you will need to do is wait for certain events to fire. For example, if your client successfully authenticated, then there will be a session_start event (I may add an auth_success or similar event later). Likewise, if authentication failed for a single mechanism, there will be a failed_auth event. If no authentication methods at all succeeded (which is what you'd be interested in), there will be a no_auth event.

因此您可以为这些事件添加处理程序,并让这些处理程序在队列中放置一个令牌,然后等待所需的令牌到达.

So you can add handlers for these events, and have those handlers place a token in a queue, and then wait for the desired token to arrive.

class ChatClient(ClientXMPP):
    def __init__(self, ...):
        ...
        self.auth_queue = queue.Queue()
        self.add_event_handler('no_auth', self.failed)

    def start(self, event):
        self.auth_queue.put('success')
        ...

    def failed(self, event):
        self.auth_queue.put('failed')


def authenticate(username, password, server):
    xmppuser = username + '@' + server
    passTester = ChatClient(xmppuser, password)
    try:
        result = passTester.auth_queue.get(timeout=10)
    except queue.Empty:
        result = 'failed'
    passTester.disconnect()
    return result == 'success'

别忘了,我们还有聊天室,地址是 slim@conference.jabber.org.

Don't forget that we also have the chat room at sleek@conference.jabber.org.

-- 兰斯

这篇关于slimxmpp 线程身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆