示例#1
0
    def __init__(self, host, connect_timeout):
        # Copy whole constructor from amqp.transport._AbstractTransport to
        # comment out one single line
        self.connected = True
        # noinspection PyUnusedLocal
        msg = None
        port = transport.AMQP_PORT

        m = transport.IPV6_LITERAL.match(host)
        if m:
            host = m.group(1)
            if m.group(2):
                port = int(m.group(2))
        else:
            if ':' in host:
                host, port = host.rsplit(':', 1)
                port = int(port)

        self.sock = None
        last_err = None
        for res in socket.getaddrinfo(host, port, 0,
                                      socket.SOCK_STREAM, SOL_TCP):
            af, socktype, proto, canonname, sa = res
            try:
                self.sock = socket.socket(af, socktype, proto)
                try:
                    set_cloexec(self.sock, True)
                except NotImplementedError:
                    pass
                self.sock.settimeout(connect_timeout)
                self.sock.connect(sa)
            except socket.error as exc:
                msg = exc
                self.sock.close()
                self.sock = None
                last_err = msg
                continue
            break

        if not self.sock:
            # Didn't connect, return the most recent error message
            raise socket.error(last_err)

        try:
            # Here is the magic: disable unlimited timeout while waiting
            # for amqp hello message

            # self.sock.settimeout(None)

            self.sock.setsockopt(SOL_TCP, socket.TCP_NODELAY, 1)
            self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)

            self._setup_transport()

            self._write(transport.AMQP_PROTOCOL_HEADER)
        except (OSError, IOError, socket.error) as exc:
            # noinspection PyProtectedMember
            if get_errno(exc) not in transport._UNAVAIL:
                self.connected = False
            raise
示例#2
0
 def test_no_args(self):
     assert not get_errno(object())
示例#3
0
 def test_args_short(self):
     exc = KeyError(34)
     assert not get_errno(exc)
示例#4
0
 def test_in_args(self):
     exc = KeyError(34, 'foo')
     exc.args = (34, 'foo')
     assert get_errno(exc) == 34
示例#5
0
 def test_has_attr(self):
     exc = KeyError('foo')
     exc.errno = 23
     assert get_errno(exc) == 23
示例#6
0
def new_transport_init(self, host, connect_timeout):

	import errno
	import re
	import socket
	import ssl

	# Jython does not have this attribute
	try:
		from socket import SOL_TCP
	except ImportError:  # pragma: no cover
		from socket import IPPROTO_TCP as SOL_TCP  # noqa

	try:
		from ssl import SSLError
	except ImportError:
		class SSLError(Exception):  # noqa
			pass

	from struct import pack, unpack

	from amqp.exceptions import UnexpectedFrame
	from amqp.utils import get_errno, set_cloexec

	_UNAVAIL = errno.EAGAIN, errno.EINTR, errno.ENOENT

	AMQP_PORT = 5672

	EMPTY_BUFFER = bytes()

	# Yes, Advanced Message Queuing Protocol Protocol is redundant
	AMQP_PROTOCOL_HEADER = 'AMQP\x01\x01\x00\x09'.encode('latin_1')

	# Match things like: [fe80::1]:5432, from RFC 2732
	IPV6_LITERAL = re.compile(r'\[([\.0-9a-f:]+)\](?::(\d+))?')

	# --------------------------------------------------------------------------
	# __init__ content:
	# --------------------------------------------------------------------------
	self.connected = True
	msg = None
	port = AMQP_PORT

	m = IPV6_LITERAL.match(host)
	if m:
		host = m.group(1)
		if m.group(2):
			port = int(m.group(2))
	else:
		if ':' in host:
			host, port = host.rsplit(':', 1)
			port = int(port)

	self.sock = None
	last_err = None
	for res in socket.getaddrinfo(host, port, 0,
	                              socket.SOCK_STREAM, SOL_TCP):
		af, socktype, proto, canonname, sa = res
		try:
			self.sock = socket.socket(af, socktype, proto)
			try:
				set_cloexec(self.sock, True)
			except NotImplementedError:
				pass
			self.sock.settimeout(connect_timeout)
			self.sock.connect(sa)
		except socket.error as exc:
			msg = exc
			self.sock.close()
			self.sock = None
			last_err = msg
			continue
		break

	if not self.sock:
		# Didn't connect, return the most recent error message
		raise socket.error(last_err)

	try:
		# self.sock.settimeout(None)
		self.sock.setsockopt(SOL_TCP, socket.TCP_NODELAY, 1)
		self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)

		self._setup_transport()

		self._write(AMQP_PROTOCOL_HEADER)
	except (OSError, IOError, socket.error) as exc:
		if get_errno(exc) not in _UNAVAIL:
			self.connected = False
		raise
示例#7
0
 def test_in_args(self):
     exc = KeyError(34, 'foo')
     exc.args = (34, 'foo')
     self.assertEqual(get_errno(exc), 34)
示例#8
0
 def test_args_short(self):
     exc = KeyError(34)
     assert not get_errno(exc)
示例#9
0
 def test_in_args(self):
     exc = KeyError(34, 'foo')
     exc.args = (34, 'foo')
     self.assertEqual(get_errno(exc), 34)
示例#10
0
 def test_has_attr(self):
     exc = KeyError('foo')
     exc.errno = 23
     self.assertEqual(get_errno(exc), 23)
示例#11
0
def new_transport_init(self, host, connect_timeout):

    import errno
    import re
    import socket
    import ssl

    # Jython does not have this attribute
    try:
        from socket import SOL_TCP
    except ImportError:  # pragma: no cover
        from socket import IPPROTO_TCP as SOL_TCP  # noqa

    try:
        from ssl import SSLError
    except ImportError:

        class SSLError(Exception):  # noqa
            pass

    from struct import pack, unpack

    from amqp.exceptions import UnexpectedFrame
    from amqp.utils import get_errno, set_cloexec

    _UNAVAIL = errno.EAGAIN, errno.EINTR, errno.ENOENT

    AMQP_PORT = 5672

    EMPTY_BUFFER = bytes()

    # Yes, Advanced Message Queuing Protocol Protocol is redundant
    AMQP_PROTOCOL_HEADER = 'AMQP\x01\x01\x00\x09'.encode('latin_1')

    # Match things like: [fe80::1]:5432, from RFC 2732
    IPV6_LITERAL = re.compile(r'\[([\.0-9a-f:]+)\](?::(\d+))?')

    # --------------------------------------------------------------------------
    # __init__ content:
    # --------------------------------------------------------------------------
    self.connected = True
    msg = None
    port = AMQP_PORT

    m = IPV6_LITERAL.match(host)
    if m:
        host = m.group(1)
        if m.group(2):
            port = int(m.group(2))
    else:
        if ':' in host:
            host, port = host.rsplit(':', 1)
            port = int(port)

    self.sock = None
    last_err = None
    for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM, SOL_TCP):
        af, socktype, proto, canonname, sa = res
        try:
            self.sock = socket.socket(af, socktype, proto)
            try:
                set_cloexec(self.sock, True)
            except NotImplementedError:
                pass
            self.sock.settimeout(connect_timeout)
            self.sock.connect(sa)
        except socket.error as exc:
            msg = exc
            self.sock.close()
            self.sock = None
            last_err = msg
            continue
        break

    if not self.sock:
        # Didn't connect, return the most recent error message
        raise socket.error(last_err)

    try:
        # self.sock.settimeout(None)
        self.sock.setsockopt(SOL_TCP, socket.TCP_NODELAY, 1)
        self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)

        self._setup_transport()

        self._write(AMQP_PROTOCOL_HEADER)
    except (OSError, IOError, socket.error) as exc:
        if get_errno(exc) not in _UNAVAIL:
            self.connected = False
        raise
示例#12
0
 def test_no_args(self):
     self.assertFalse(get_errno(object()))
示例#13
0
 def test_args_short(self):
     exc = KeyError(34)
     self.assertFalse(get_errno(exc))
示例#14
0
 def test_has_attr(self):
     exc = KeyError('foo')
     exc.errno = 23
     assert get_errno(exc) == 23
示例#15
0
 def test_args_short(self):
     exc = KeyError(34)
     self.assertFalse(get_errno(exc))
示例#16
0
 def test_in_args(self):
     exc = KeyError(34, 'foo')
     exc.args = (34, 'foo')
     assert get_errno(exc) == 34
示例#17
0
 def test_no_args(self):
     self.assertFalse(get_errno(object()))
示例#18
0
 def test_no_args(self):
     assert not get_errno(object())
示例#19
0
 def test_has_attr(self):
     exc = KeyError('foo')
     exc.errno = 23
     self.assertEqual(get_errno(exc), 23)