Updated xmlstream.filesocket.

This commit is contained in:
Lance Stout 2010-08-27 11:29:48 -04:00
parent a2c515bc97
commit 6677df39f2
2 changed files with 26 additions and 12 deletions

View file

@ -5,21 +5,36 @@
See the file LICENSE for copying permission. See the file LICENSE for copying permission.
""" """
from socket import _fileobject from socket import _fileobject
import socket import socket
class filesocket(_fileobject): class FileSocket(_fileobject):
def read(self, size=4096): """
data = self._sock.recv(size) Create a file object wrapper for a socket to work around
if data is not None: issues present in Python 2.6 when using sockets as file objects.
return data
The parser for xml.etree.cElementTree requires a file, but we will
be reading from the XMPP connection socket instead.
"""
def read(self, size=4096):
"""Read data from the socket as if it were a file."""
data = self._sock.recv(size)
if data is not None:
return data
class Socket26(socket._socketobject): class Socket26(socket._socketobject):
def makefile(self, mode='r', bufsize=-1): """
"""makefile([mode[, bufsize]]) -> file object A custom socket implementation that uses our own FileSocket class
Return a regular file object corresponding to the socket. The mode to work around issues in Python 2.6 when using sockets as files.
and bufsize arguments are as for the built-in open() function.""" """
return filesocket(self._sock, mode, bufsize)
def makefile(self, mode='r', bufsize=-1):
"""makefile([mode[, bufsize]]) -> file object
Return a regular file object corresponding to the socket. The mode
and bufsize arguments are as for the built-in open() function."""
return FileSocket(self._sock, mode, bufsize)

View file

@ -139,8 +139,7 @@ class XMLStream(object):
self.socket = ssl.wrap_socket(self.socket, ssl_version=ssl.PROTOCOL_TLSv1, do_handshake_on_connect=False) self.socket = ssl.wrap_socket(self.socket, ssl_version=ssl.PROTOCOL_TLSv1, do_handshake_on_connect=False)
self.socket.do_handshake() self.socket.do_handshake()
if sys.version_info < (3,0): if sys.version_info < (3,0):
from . filesocket import filesocket self.filesocket = filesocket.FileSocket(self.socket)
self.filesocket = filesocket(self.socket)
else: else:
self.filesocket = self.socket.makefile('rb', 0) self.filesocket = self.socket.makefile('rb', 0)
return True return True