From 99fda869bb3d7fa18721fe2c21eb61557603cbcb Mon Sep 17 00:00:00 2001 From: MarcDufresne Date: Fri, 2 Sep 2016 11:44:47 -0400 Subject: [PATCH 1/7] Added support for AMQP connection string --- amqpy/connection.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/amqpy/connection.py b/amqpy/connection.py index a27ca75..3f81e49 100644 --- a/amqpy/connection.py +++ b/amqpy/connection.py @@ -2,6 +2,8 @@ """ from __future__ import absolute_import, division, print_function +from urlparse import urlparse, unquote + __metaclass__ = type import logging import socket @@ -55,7 +57,13 @@ def __init__(self, host='localhost', port=5672, ssl=None, connect_timeout=None, If you are using SSL, make sure the correct port number is specified (usually 5671), as the default of 5672 is for non-SSL connections. - :param str host: host + You can define an AMQP connection string as the host, this will be used to set + the `host`, `port`, `userid`, `password` and `virtual_host`. The connection string follows + this format: + + `amqp://[userid:password@]host[:port][/virtual_host]` + + :param str host: host or amqp connection string :param int port: port :param ssl: dict of SSL options passed to :func:`ssl.wrap_socket()`, None to disable SSL :param float connect_timeout: connect timeout @@ -111,6 +119,15 @@ def __init__(self, host='localhost', port=5672, ssl=None, connect_timeout=None, self._heartbeat_final = 0 # final heartbeat interval after negotiation self._heartbeat_server = None + # detect amqp connection string + if host.startswith('amqp://'): + parts = urlparse("http://" + host[7:]) + host = unquote(parts.hostname or '') or None + port = parts.port + userid = unquote(parts.username or '') or None + password = unquote(parts.password or '') or None + virtual_host = unquote(parts.path or '/') + # save connection parameters self._host = host self._port = port From fa6150ace16bb4157afa4cae06c90f94f7c9715f Mon Sep 17 00:00:00 2001 From: MarcDufresne Date: Fri, 2 Sep 2016 11:58:06 -0400 Subject: [PATCH 2/7] Setting appropriate defaults --- amqpy/connection.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/amqpy/connection.py b/amqpy/connection.py index 3f81e49..b9aaacd 100644 --- a/amqpy/connection.py +++ b/amqpy/connection.py @@ -123,9 +123,9 @@ def __init__(self, host='localhost', port=5672, ssl=None, connect_timeout=None, if host.startswith('amqp://'): parts = urlparse("http://" + host[7:]) host = unquote(parts.hostname or '') or None - port = parts.port - userid = unquote(parts.username or '') or None - password = unquote(parts.password or '') or None + port = parts.port or 5672 + userid = unquote(parts.username or '') or 'guest' + password = unquote(parts.password or '') or 'guest' virtual_host = unquote(parts.path or '/') # save connection parameters From 84217744e500b2bf596924dae814f9c093915035 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Dufresne?= Date: Tue, 7 Feb 2017 17:41:50 -0500 Subject: [PATCH 3/7] Fixed urllib compatibility with python 3 --- amqpy/connection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/amqpy/connection.py b/amqpy/connection.py index b9aaacd..2d04ebd 100644 --- a/amqpy/connection.py +++ b/amqpy/connection.py @@ -2,7 +2,7 @@ """ from __future__ import absolute_import, division, print_function -from urlparse import urlparse, unquote +from future.moves.urllib.parse import urlparse, urlencode __metaclass__ = type import logging From 5c42d23ff08beb4b4ec049919a97a7080bfa4065 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Dufresne?= Date: Thu, 9 Feb 2017 11:23:56 -0500 Subject: [PATCH 4/7] Added future requirement for urllib --- setup.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/setup.py b/setup.py index b7d8f8a..d6e83e9 100644 --- a/setup.py +++ b/setup.py @@ -58,7 +58,10 @@ def long_description(): packages=find_packages(exclude=['ez_setup', 'tests', 'tests.*']), package_data=package_data, setup_requires=['six>=1.0'], - install_requires=['six>=1.0'], + install_requires=[ + 'six>=1.0', + 'future>=0.16.0', + ], tests_require=['pytest>=2.6'], classifiers=classifiers, keywords=keywords From 27c038fca2ddf48c267251e26ad53626c87d27f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Dufresne?= Date: Thu, 9 Feb 2017 12:13:11 -0500 Subject: [PATCH 5/7] Forgot unquote --- amqpy/connection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/amqpy/connection.py b/amqpy/connection.py index 2d04ebd..025dd8e 100644 --- a/amqpy/connection.py +++ b/amqpy/connection.py @@ -2,7 +2,7 @@ """ from __future__ import absolute_import, division, print_function -from future.moves.urllib.parse import urlparse, urlencode +from future.moves.urllib.parse import urlparse, urlencode, unquote __metaclass__ = type import logging From 9cfd9609606e53b25ba9c5a4b0ed4f8a31bcc60b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Dufresne?= Date: Thu, 9 Feb 2017 12:15:38 -0500 Subject: [PATCH 6/7] Removed unused urlencode --- amqpy/connection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/amqpy/connection.py b/amqpy/connection.py index 025dd8e..7efb065 100644 --- a/amqpy/connection.py +++ b/amqpy/connection.py @@ -2,7 +2,7 @@ """ from __future__ import absolute_import, division, print_function -from future.moves.urllib.parse import urlparse, urlencode, unquote +from future.moves.urllib.parse import urlparse, unquote __metaclass__ = type import logging From 68c26be385e0ec631300b94b2e23464b2c8664f5 Mon Sep 17 00:00:00 2001 From: jeromevlarouche Date: Wed, 22 Nov 2017 11:24:08 -0500 Subject: [PATCH 7/7] remove / from virtual host virtual_host should not start by a `/`, so we start at the second char of the path. --- amqpy/connection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/amqpy/connection.py b/amqpy/connection.py index 7efb065..cb88e41 100644 --- a/amqpy/connection.py +++ b/amqpy/connection.py @@ -126,7 +126,7 @@ def __init__(self, host='localhost', port=5672, ssl=None, connect_timeout=None, port = parts.port or 5672 userid = unquote(parts.username or '') or 'guest' password = unquote(parts.password or '') or 'guest' - virtual_host = unquote(parts.path or '/') + virtual_host = unquote(parts.path[1:] or '/') # save connection parameters self._host = host