sleekxmpp threaded authentication

2024/10/13 3:30:01

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_SSLv3self.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 + '@' + serverpassTester = ChatClient(xmppuser, password)passTester.disconnect(wait = True)authenticated = passTester.authenticatedreturn 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 + '@' + serverpassTester = ChatClient(xmppuser, password)passTester.message('bogusName', 'ladfhkjdglkhjdfg')passTester.disconnect(wait = True)authenticated = passTester.authenticatedreturn authenticated

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!

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"?

Answer

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.

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 + '@' + serverpassTester = ChatClient(xmppuser, password)try:result = passTester.auth_queue.get(timeout=10)except queue.Empty:result = 'failed'passTester.disconnect()return result == 'success'

Don't forget that we also have the chat room at [email protected].

-- Lance

https://en.xdnf.cn/q/118126.html

Related Q&A

DoxyPy - Member (variable) of namespace is not documented

I get the error message warning: Member constant1 (variable) of namespace <file_name> is not documented. for my doxygen (doxypy) documentation. I have documented the file and all functions and cl…

to_csv append mode is not appending to next new line

I have a csv called test.csv that looks like:accuracy threshold trainingLabels abc 0.506 15000 eew 18.12 15000And then a dataframe called summaryDF that looks like:accu…

Optional keys in string formats using % operator?

Is is possible to have optional keys in string formats using % operator? I’m using the logging API with Python 2.7, so I cant use Advanced String Formatting.My problem is as follow:>>> impor…

HDF5 headers missing in installation of netCDF4 module for Python

I have attempted to install the netCDF4 module several times now and I keep getting the same error:Traceback (most recent call last):File "<string>", line 17, in <module>File &quo…

How to catch network failures while invoking get() method through Selenium and Python?

I am using Chrome with selenium and the test run well, until suddenly internet/proxy connection is down, then browser.get(url) get me this:If I reload the page 99% it will load fine, what is the proper…

Pandas crosstab with own function

I have a function which takes two inputs and returns a float e.g. my_func(A, B) = 0.5. I have a list of possible inputs: x = [A, B, C, D, E, F].I want to produce a square matrix (in this case 6 by 6) …

Using a custom threshold value with tf.contrib.learn.DNNClassifier?

Im working on a binary classification problem and Im using the tf.contrib.learn.DNNClassifier class within TensorFlow. When invoking this estimator for only 2 classes, it uses a threshold value of 0.5 …

How to remove certain characters from a variable? (Python)

Lets suppose I have a variable called data. This data variable has all this data and I need to remove certain parts of it while keeping most of it. Lets say I needed to remove all the , (commas) in thi…

criticism this python code (crawler with threadpool)

how good this python code ? need criticism) there is a error in this code, some times script do print "ALL WAIT - CAN FINISH!" and freeze (no more actions are happend..) but i cant find reas…

cx_Freeze executable not displaying matplotlib figures

I am using Python 3.5 and I was able to create an executable using cx_Freeze but whenever I try to run the executable it runs without error but it cannot display any matplotlib figure. I have used Tkin…