* python 2.6 compatibility

This commit is contained in:
Nathan Fritz 2010-01-08 06:03:02 +00:00
parent 0af468b435
commit a8ff3586d3
5 changed files with 40 additions and 13 deletions

View file

@ -19,7 +19,7 @@
along with SleekXMPP; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
"""
from __future__ import absolute_import
from __future__ import absolute_import, unicode_literals
from . basexmpp import basexmpp
from xml.etree import cElementTree as ET
from . xmlstream.xmlstream import XMLStream
@ -201,7 +201,10 @@ class ClientXMPP(basexmpp, XMLStream):
for sasl_mech in sasl_mechs:
self.features.append("sasl:%s" % sasl_mech.text)
if 'sasl:PLAIN' in self.features:
self.send("""<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='PLAIN'>%s</auth>""" % base64.b64encode(b'\x00' + bytes(self.username, 'utf-8') + b'\x00' + bytes(self.password, 'utf-8')).decode('utf-8'))
if sys.version_info < (3,0):
self.send("""<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='PLAIN'>%s</auth>""" % base64.b64encode(b'\x00' + bytes(self.username) + b'\x00' + bytes(self.password)).decode('utf-8'))
else:
self.send("""<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='PLAIN'>%s</auth>""" % base64.b64encode(b'\x00' + bytes(self.username, 'utf-8') + b'\x00' + bytes(self.password, 'utf-8')).decode('utf-8'))
else:
logging.error("No appropriate login method.")
self.disconnect()

View file

@ -17,7 +17,7 @@
along with SleekXMPP; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
"""
from __future__ import with_statement
from __future__ import with_statement, unicode_literals
from xml.etree import cElementTree as ET

View file

@ -1,5 +1,8 @@
from . import base
import queue
try:
import queue
except ImportError:
import Queue as queue
import logging
from .. stanzabase import StanzaBase

View file

@ -25,7 +25,7 @@ class MatchXMLMask(base.MatcherBase):
#TODO require namespaces
if source == None: #if element not found (happens during recursive check below)
return False
if type(maskobj) == type(str()): #if the mask is a string, make it an xml obj
if not hasattr(maskobj, 'attrib'): #if the mask is a string, make it an xml obj
try:
maskobj = cElementTree.fromstring(maskobj)
except ExpatError:

View file

@ -1,5 +1,8 @@
from __future__ import with_statement
import queue
from __future__ import with_statement, unicode_literals
try:
import queue
except ImportError:
import Queue as queue
from . import statemachine
from . stanzabase import StanzaBase
from xml.etree import cElementTree
@ -15,10 +18,15 @@ import xml.sax.saxutils
HANDLER_THREADS = 1
ssl_support = True
try:
import ssl
except ImportError:
ssl_support = False
#try:
import ssl
#except ImportError:
# ssl_support = False
import sys
if sys.version_info < (3, 0):
#monkey patch broken filesocket object
from . import filesocket
socket._fileobject = filesocket.filesocket
class RestartStream(Exception):
@ -89,11 +97,13 @@ class XMLStream(object):
self.use_tls = use_tls
self.state.set('is client', True)
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.settimeout(None)
if self.use_ssl and self.ssl_support:
logging.debug("Socket Wrapped for SSL")
self.socket = ssl.wrap_socket(self.socket)
try:
self.socket.connect(self.address)
#self.filesocket = self.socket.makefile('rb', 0)
self.filesocket = self.socket.makefile('rb', 0)
self.state.set('connected', True)
return True
@ -111,7 +121,11 @@ class XMLStream(object):
self.realsocket = self.socket
self.socket = ssl.wrap_socket(self.socket, ssl_version=ssl.PROTOCOL_TLSv1, do_handshake_on_connect=False)
self.socket.do_handshake()
self.filesocket = self.socket.makefile('rb', 0)
if sys.version_info < (3,0):
from . filesocket import filesocket
self.filesocket = filesocket(self.socket)
else:
self.filesocket = self.socket.makefile('rb', 0)
return True
else:
logging.warning("Tried to enable TLS, but ssl module not found.")
@ -180,6 +194,7 @@ class XMLStream(object):
"Parses the incoming stream, adding to xmlin queue as it goes"
#build cElementTree object from expat was we go
#self.filesocket = self.socket.makefile('rb', 0)
#print self.filesocket.read(1024) #self.filesocket._sock.recv(1024)
edepth = 0
root = None
for (event, xmlobj) in cElementTree.iterparse(self.filesocket, (b'end', b'start')):
@ -208,11 +223,14 @@ class XMLStream(object):
logging.debug("SEND: %s" % data)
try:
self.socket.send(bytes(data, "utf-8"))
except TypeError:
self.socket.send(bytes(data))
#except socket.error,(errno, strerror):
except:
self.state.set('connected', False)
if self.state.reconnect:
logging.error("Disconnected. Socket Error.")
traceback.print_exc()
self.disconnect(reconnect=True)
return False
return True
@ -277,7 +295,10 @@ class XMLStream(object):
except queue.Empty:
event = None
if event is not None:
etype, handler, *args = event
etype = event[0]
handler = event[1]
args = event[2:]
#etype, handler, *args = event #python 3.x way
if etype == 'stanza':
try:
handler.run(args[0])