From 8e166ae8264a7c9f98e766c8635faa94cd075eb8 Mon Sep 17 00:00:00 2001 From: Mike Kazantsev Date: Mon, 11 Feb 2013 13:56:04 +0100 Subject: skype: cleanup of the send() code Use socket.sendall(), as send() is not guaranteed to send all the data passed (though it should generally work that way with blocking sockets). Use more robust, obvious and idiomatic loop conditions. Cleanup redundant imports and variables. --- protocols/skype/skyped.py | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) (limited to 'protocols/skype/skyped.py') diff --git a/protocols/skype/skyped.py b/protocols/skype/skyped.py index 76368aba..57b2f9ce 100644 --- a/protocols/skype/skyped.py +++ b/protocols/skype/skyped.py @@ -123,35 +123,33 @@ def skype_idle_handler(skype): time.sleep(1) return True -def send(sock, txt): +def send(sock, txt, tries=10): global options - from time import sleep - count = 1 - done = False if hasgobject: - while (not done) and (count < 10): + for attempt in xrange(1, tries+1): try: - sock.send(txt) - done = True + sock.sendall(txt) except Exception, s: - count += 1 - dprint("Warning, sending '%s' failed (%s). count=%d" % (txt, s, count)) - sleep(1) - if not done: + dprint("Warning, sending '%s' failed (%s). count=%d" % (txt, s, attempt)) + time.sleep(1) + else: + break + else: options.conn.close() else: - while (not done) and (count < 10) and options.conn: + for attempt in xrange(1, tries+1): + if not options.conn: break if wait_for_lock(options.lock, 3, 10, "socket send"): try: - if options.conn: sock.send(txt) + if options.conn: sock.sendall(txt) options.lock.release() - done = True except Exception, s: options.lock.release() - count += 1 dprint("Warning, sending '%s' failed (%s). count=%d" % (txt, s, count)) - sleep(1) - if not done: + time.sleep(1) + else: + break + else: if options.conn: options.conn.close() options.conn = False -- cgit v1.2.3